【问题标题】:Enterprise Library 5.0: Work with many categories in logsEnterprise Library 5.0:处理日志中的许多类别
【发布时间】:2011-12-15 17:04:43
【问题描述】:

有一些代码:

private const string FORMAT_TEXT = 
   "{timestamp}: [{category} ({win32ThreadId}-{threadName}) {severity}] |{title}|: {message}";
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "trace.log");
var builder = new ConfigurationSourceBuilder();


builder.ConfigureLogging()
         .WithOptions.DoNotRevertImpersonation()
         .LogToCategoryNamed("Category1")
         .SendTo.FlatFile("MyMessages1")
         .FormatWith(new FormatterBuilder()
            .TextFormatterNamed("Text Formatter 1").UsingTemplate(FORMAT_TEXT))
         .ToFile(path)
         .LogToCategoryNamed("Category2")
         .SendTo.FlatFile("MyMessages2")
         .FormatWith(new FormatterBuilder()
            .TextFormatterNamed("Text Formatter 2").UsingTemplate(FORMAT_TEXT))
         .ToFile(path);

var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

for (int i = 0; i < 10; i++)
{
   Logger.Write("Error message: " + i.ToString(), "Category1");
   Logger.Write("Error message: " + i.ToString(), "Category2");
}

Console.ReadKey();

它会生成两个日志文件:trace.log 用于 Category1,65a25bb0-4c42-430d-b2b7-9bd2c8ea41e1trace.log 用于 Category2。我想记录到一个文件 trace.log。怎么了?

【问题讨论】:

    标签: c# .net logging enterprise-library


    【解决方案1】:

    您想使用 SharedListener 以便两个类别都记录到同一个侦听器。在您的代码中,您正在创建两个单独的侦听器,它们都记录到同一个文件(这会导致文件锁定问题)。

    试试:

    builder.ConfigureLogging()
             .WithOptions.DoNotRevertImpersonation()
             .LogToCategoryNamed("Category1")
                 .SendTo.FlatFile("MyMessages1")
                 .FormatWith(new FormatterBuilder()
                    .TextFormatterNamed("Text Formatter 1").UsingTemplate(FORMAT_TEXT))
                 .ToFile(path)
             .LogToCategoryNamed("Category2")
                 .SendTo.SharedListenerNamed("MyMessages1");
    

    【讨论】:

      猜你喜欢
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多