C# Enterprise Library 5.0:处理日志中的许多类别

C# Enterprise Library 5.0:处理日志中的许多类别,c#,.net,logging,enterprise-library,C#,.net,Logging,Enterprise Library,有一些代码: private const string FORMAT_TEXT = "{timestamp}: [{category} ({win32ThreadId}-{threadName}) {severity}] |{title}|: {message}"; string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "trace.log"); var builder = new ConfigurationS

有一些代码:

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();
private const字符串格式\u 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(“类别1”)
.SendTo.FlatFile(“MyMessages1”)
.FormatWith(新FormatterBuilder()
.TextFormatterNamed(“文本格式化程序1”)。使用模板(格式\文本))
.ToFile(路径)
.LogToCategoryNamed(“类别2”)
.SendTo.FlatFile(“MyMessages2”)
.FormatWith(新FormatterBuilder()
.TextFormatterNamed(“文本格式化程序2”)。使用模板(格式\文本))
.ToFile(路径);
var configSource=new DictionaryConfigurationSource();
builder.updateConfiguration WithReplace(配置源);
EnterpriseLibraryContainer.Current=EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
对于(int i=0;i<10;i++)
{
Write(“错误消息:+i.ToString(),“Category1”);
Write(“错误消息:+i.ToString(),“Category2”);
}
Console.ReadKey();

它生成两个日志文件:类别1的trace.log,类别2的65a25bb0-4c42-430d-b2b7-9bd2c8ea41e1trace.log。我想登录到一个文件trace.log。怎么了?

您想使用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");