Tutorial :Group log messages



Question:

I got a web application in which i've implemented logging (using Common.Logging), it now saves the log messages to the database. But in one part of the system, an import process, i would like to present only the log messages related to that import process (which the user instantiated) and not the accumulated log messages.

below you see the code for my ImportProcessor, which depends on ILog (from Common.Loggning) and some import components, which also depends on ILog and some other stuff..

ILog is injected with a DI (autofac) where the Logname is automaticly resolved.

public class ImportProcessor  {      private readonly ILog log;      private readonly IImportFetcher fetcher;      private readonly IImportFormatter formatter;      private readonly IEnumerable<IImportFilter> filters;      private readonly IEnumerable<IImportExporter> exporters;        internal ImportProcessor(ILog log, IImportFetcher fetcher, IImportFormatter formatter, IEnumerable<IImportFilter> filters, IEnumerable<IImportExporter> exporters)      {          this.log = log;          this.fetcher = fetcher;          this.formatter = formatter;          this.filters = filters;          this.exporters = exporters;      }        public void Execute()      {          log.Info("Import starts");            var rawdata = fetcher.Fetch();          var importDatas = formatter.Transform(rawdata);            foreach (var filter in filters)          {              importDatas = filter.Filter(importDatas);          }            foreach (var exporter in exporters)          {              exporter.Export(importDatas);          }            log.Info("Import done");                  }  }  

Do you got any good suggestion on how to group all Log messages related to that ImportProcessor?


Solution:1

You could try using Enterprise Library Logging:

http://msdn.microsoft.com/en-us/library/dd139916.aspx

Using EntLib, logs can subscribe to different events. Allowinng you to put all events in one place, while at the same time placing specific events in a separate log.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »