希望在现有(几乎是遗留的)应用程序中找到日志记录的替代品。这将基于eventId记录事件,并将dll配置为事件消息文件(记录到事件日志和消息队列中)。整个日志记录依赖于事件id,因为一些记录的事件被上载到企业,在企业中,它们基于事件id进行国际化。在此日志基础设施中,还添加了一些文件日志,这些日志不使用此事件消息文件,而是使用记录的普通字符串(内部具有文件滚动和存档支持)。
我一直在寻找一个日志框架,它将具有基于id (Error(id,object[] args))和“普通”消息(Error(string message,object[] args))的日志记录标准接口。
log4net和nlog似乎都没有这样的接口。
有没有现成的日志框架支持这一点,或者我必须自己构建。或者以某种方式修改log4net/nlog来实现这一点。
谢谢,弗洛林
发布于 2010-12-06 01:09:17
查看Enterprise Library中的logging应用程序块:http://msdn.microsoft.com/en-us/library/cc511708.aspx
发布于 2010-12-13 02:07:46
有关log4net“扩展”,请参阅此link,该扩展允许您相当容易地将事件ids添加到日志消息中。
以下是log4net链接中的一段简短片段:
public interface IEventIDLog : ILog
{
void Info(int eventId, object message);
void Info(int eventId, object message, Exception t);
}
public class EventIDLogImpl : LogImpl, IEventIDLog
{
/// <summary>
/// The fully qualified name of this declaring type not the type of any subclass.
/// </summary>
private readonly static Type ThisDeclaringType = typeof(EventIDLogImpl);
public EventIDLogImpl(ILogger logger) : base(logger)
{
}
#region Implementation of IEventIDLog
public void Info(int eventId, object message)
{
Info(eventId, message, null);
}
public void Info(int eventId, object message, System.Exception t)
{
if (this.IsInfoEnabled)
{
LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t);
loggingEvent.Properties["EventID"] = eventId;
Logger.Log(loggingEvent);
}
}
}我看不出有什么理由不能用NLog做类似的事情。这是NLog的github源代码库中的一个link to some examples of extending the NLog Logger。
我还会注意到,System.Diagnostics.TraceSource还支持日志记录EventId。如果考虑使用System.Diagnostics,可能还需要考虑使用Ukadc.Diagnostics来获得类似于使用log4net和NLog的强大格式化功能。
发布于 2010-12-13 02:18:53
您还可以将遗留应用程序的日志记录集成到您的正常日志记录基础设施中。这可以通过使用.net Common.Logging的routing feature轻松实现,它是日志记录框架的抽象(它支持企业库、log4net、NLog和System.Trace)。
https://stackoverflow.com/questions/4359912
复制相似问题