【发布时间】:2012-12-09 02:25:00
【问题描述】:
我正在使用 Microsoft Enterprise Library 5.0 Logging 块,我想知道 LogWriter 和 Logger 类之间的区别。我有一些自定义跟踪侦听器,我已构建用于我的日志记录,我想知道使用 Logger 与 LogWriter 是否会产生任何影响。
来自 MSDN http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.logwriter.aspx
要将日志消息写入默认配置,请使用 Logger 外观。如果您需要使用自定义配置写入日志消息,请仅创建 LogWriter 的实例。
示例代码#1:
namespace ExampleOne
{
using System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
public class Program
{
private static LogWriter logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
public static void Main(string[] args)
{
try
{
throw new Exception("My test exception message");
}
catch (Exception ex)
{
LogEntry logEntry = new LogEntry();
logEntry.Message = "Error: " + ex.ToString();
logEntry.Categories.Add("General");
logEntry.Severity = TraceEventType.Error;
logger.Write(logEntry);
}
}
}
}
示例代码#2:
namespace ExampleTwo
{
using System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
public class Program
{
public static void Main(string[] args)
{
try
{
throw new Exception("My test exception message");
}
catch (Exception ex)
{
LogEntry logEntry = new LogEntry();
logEntry.Message = "Error: " + ex.ToString();
logEntry.Categories.Add("General");
logEntry.Severity = TraceEventType.Error;
Logger.Write(logEntry);
}
}
}
}
【问题讨论】:
-
您在寻找什么样的效果(性能等)?我建议对 LogEntry 的构造函数进行反编译,因为这会告诉你在使用它之前底层 LogWriter 会发生什么。
-
我真的不是在寻找任何效果。我只是想知道一种与另一种相比是否是正确的使用方法。
标签: logging customization enterprise-library-5 tracelistener