看到這個link的log4net的「延伸」,使您可以合理地輕鬆添加事件ID記錄消息。
下面是來自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所做的強大格式化功能。
有關TraceEvent-Method的id的一些附加信息可用[here](http://stackoverflow.com/questions/17590453/what-should-i-identify-with-the-id-argument-in- tracesource-traceevent法) – Sonic78 2016-10-25 11:55:54