How do I configure the Microsoft Enterprise Journal Logging application block to handle any category of logging?

I am trying to use Common Infrastructure Libraries for.NET (Common.Logging) with corporate library 4.1. Launch application block. According to docs , this is supported.

The problem I am facing is that when I try to write something, for example (in this example I am using an ASP.NET MVC application):

public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; ILog log = LogManager.GetCurrentClassLogger(); log.Info("Hello world!"); return View(); } 

Instead of receiving a log message, I get an error message in the event log:

Message: There is no explicit mapping for the categories "TestApp.Controllers.HomeController".

Well, it seems that in Common.Logging there is no option to set the default category, and I cannot figure out how to configure LoggingConfiguration to accept any category, even if it is not defined.

Here is a snippet of my LoggingConfiguration setup:

 <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"> ... <categorySources> <add switchValue="All" name="General"> <listeners> <add name="Formatted EventLog TraceListener" /> <add name="Email TraceListener" /> </listeners> </add> </categorySources> <specialSources> <allEvents switchValue="All" name="All Events" /> <notProcessed switchValue="All" name="Unprocessed Category" /> <errors switchValue="All" name="Logging Errors &amp; Warnings"> <listeners> <add name="Formatted EventLog TraceListener" /> </listeners> </errors> </specialSources> </loggingConfiguration> 

I tried setting logWarningsWhenNoCategoriesMatch to false, but it just silences the warning - it does not do the logging work.

I also tried to get rid of the special source <notProcessed switchValue="All" .../> , but this also has no effect.

Does anyone know if there is a way to make this work? Thanks!

+4
source share
2 answers

When calling LogManager.GetCurrentClassLogger() Common.Logging will use the type of your calling class as a category:

 public static ILog GetCurrentClassLogger() { StackFrame frame = new StackFrame(1, false); return Adapter.GetLogger(frame.GetMethod().DeclaringType); } 

If you want to use this approach (although the recommendation should not use this method excessively, since it checks the StackFrame), then yes, you will need to create a category for each class from which you register. This recreates the hierarchical loggers that are used in Log4Net. But this approach is not ideal because it is not supported.

You can get around this with the LogManager.GetLogger(string name) overload instead of GetCurrentClassLogger . It seems that the name that is being passed will be used as a category to enter the corporate library. Thus, you can define the category name as a constant and pass it to the GetLogger method and use your entire application in the same Enterprise Library category.

+3
source

The special source "notProcessed" will match any elements that do not match the source of the category, so you can use them to find them. Here is more detailed information: https://entlib.codeplex.com/discussions/215189

 <notProcessed switchValue="All" name="Unprocessed Category"/> <listeners> <add name="Rolling Flat File Trace Listener Unprocessed"/> </listeners> </notProcessed> 
+4
source

Source: https://habr.com/ru/post/1344419/


All Articles