Enterprise Library 5.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要使用 SharedListener 以便两个类别都记录到同一个侦听器。在您的代码中,您正在创建两个单独的侦听器,它们都记录到同一文件(这会导致文件锁定问题)。
尝试:
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: