开发者

.net logging with eventid support

开发者 https://www.devze.com 2023-01-28 06:12 出处:网络
Looking to find a replacement for the logging in an existing (almost legacy) application. This logs events based on eventId and has a dll configured as event message file (logging is done to event log

Looking to find a replacement for the logging in an existing (almost legacy) application. This logs events based on eventId and has a dll configured as event message file (logging is done to event log and a message queue). The whole logging depends on event id as some logged events are uploaded to enterprise where they are internationalized based on the event id. To this logging infrastructure there was also added some file logging which does not use this event message file but rather logged plain strings (in house with file rollover and archiving support).

I was looking for an logging framework that will have standard interface for logging based on id (Error(id, object[] args)) and "plain" messages (Error(string开发者_如何学编程 message, object[] args)).

Neither log4net nor nlog seem to have such interfaces.

Is there any off the shelve logging framework supporting this or do i have to build by own. Or adapt somehow log4net/nlog for this.

thanks, florin


Look at the logging application block in Enterprise Library: http://msdn.microsoft.com/en-us/library/cc511708.aspx


See this link for a log4net "extension" that allows you to reasonably easily add event ids to log messages.

Here is an abbreviated snippet from the log4net link:

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

I don't see any reason why you could not do something similar with NLog. Here is a link to some examples of extending the NLog Logger in NLog's github source repository.

I will also note that System.Diagnostics.TraceSource also supports logging EventId. If you consider System.Diagnostics, you might also want to consider using Ukadc.Diagnostics to get some strong formatting capability similar to what you can do with log4net and NLog.


You could also integrate the logging of the legacy application to your normal logging infrastructure. This can be easily achieved by using the routing feature of .net Common.Logging which is an abstraction of logging frameworks (it supports enteprise lib, log4net, NLog, System.Trace).


The TraceSource.TraceEvent-Methode provides the possibility to pass a numeric identifier for a log event and also methods you can use to log "plain" messages.

0

精彩评论

暂无评论...
验证码 换一张
取 消