统一日志记录不写入文件

Unify logging not writing to file

我有以下运行良好的示例代码:

public static void Main()
{
    var traceListener = new FlatFileTraceListener("unifylogging.log",
        "--------------------",
        "-----------------------------");

    var config = new LoggingConfiguration();
    config.AddLogSource("My Log Source", SourceLevels.All, true).AddTraceListener(traceListener);

    var logger = new LogWriter(config);

    logger.Write("This is a test");
}

但是,如果我将 logger.Write 调用更改为直接从手册中提取的调用,它不会写任何内容:

logger.Write("Log entry with a category, priority, event ID,severity, and title.",
    "General",
    8,
    9003,
    TraceEventType.Warning,
    "Logging Block Examples");

我也尝试过使用 ConsoleTraceListener 以及 Write 方法的一些其他重载,包括:

唯一有效的是 Write(string)

NB The suggested duplicate is with regards to Workflow Foundation and seems to be about the System.Diagnostics.Trace tracing. This is about Unify Logging.

原来有一些奇怪的命名。 .AddLogSource(string name) 方法中的 name 参数 实际上 定义了此配置将收集的类别。

因此,通过更改类别以匹配日志源,如下所示:

public static void Main()
{
    var traceListener = new ConsoleTraceListener();

    var config = new LoggingConfiguration();

    config.AddLogSource("My Log Source", SourceLevels.All, true).AddTraceListener(traceListener);

    var logger = new LogWriter(config);

    logger.Write("This is a log.", "My Log Source");
}

将生成预期的日志。