Enterprise Library 5.0:处理日志中的多个类别

发布于 2024-12-12 06:49:51 字数 1375 浏览 0 评论 0原文

有一些代码:

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();

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

There is some code:

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();

It produces two log files: trace.log for Category1 and 65a25bb0-4c42-430d-b2b7-9bd2c8ea41e1trace.log for Category2. I want logging to one file trace.log. What is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-12-19 06:49:51

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

You want to use a SharedListener so that both categories log to the same listener. In your code you are creating two separate listeners that both log to the same file (which is causing file locking issues).

Try:

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文