Recognos / Metrics.NET

The Metrics.NET library provides a way of instrumenting applications with custom metrics (timers, histograms, counters etc) that can be reported in various ways and can provide insights on what is happening inside a running application.
Apache License 2.0
639 stars 110 forks source link

Automatic load of EntLib logger #46

Open jruizaranguren opened 7 years ago

jruizaranguren commented 7 years ago

I'm trying to enable Metrics.Net 0.4.4 in a web project using Nancy 1.4.3. I'm using the following activation code in ApplicationStartup method:

    Metric.Config
        .WithReporting(r =>
        {
            r.WithConsoleReport(TimeSpan.FromSeconds(30));
        })
        .WithNancy(pipelines);

I have a reference to Enterprise Library logging dll's (4.0.0) because a third party library makes use of them. I'm not using them directly and I do not have any configuration of those libraries in my web.config.

When I launch the web service I'm getting a System.TypeInitializationException exception. It seems Metrics.Net is trying to load the EntLib logger:

   in Microsoft.Practices.EnterpriseLibrary.Logging.Logger.get_Writer()
   in Microsoft.Practices.EnterpriseLibrary.Logging.Logger.ShouldLog(LogEntry log)
   in lambda_method(Closure , String , Int32 )
   in Metrics.Logging.LogProviders.EntLibLogProvider.EntLibLogger.Log(LogLevel logLevel, Func`1 messageFunc, Exception exception, Object[] formatParameters) in C:\Work\Metrics.NET\Src\Metrics\App_Packages\LibLog.4.2\LibLog.cs:line 1412
   in Metrics.Logging.LoggerExecutionWrapper.Log(LogLevel logLevel, Func`1 messageFunc, Exception exception, Object[] formatParameters) in C:\Work\Metrics.NET\Src\Metrics\App_Packages\LibLog.4.2\LibLog.cs:line 708
   in Metrics.Logging.LogExtensions.IsErrorEnabled(ILog logger) in C:\Work\Metrics.NET\Src\Metrics\App_Packages\LibLog.4.2\LibLog.cs:line 141
   in Metrics.Logging.LogExtensions.ErrorException(ILog logger, String message, Exception exception, Object[] formatParams) in C:\Work\Metrics.NET\Src\Metrics\App_Packages\LibLog.4.2\LibLog.cs:line 230

How could I prevent Metrics.Net trying to load EntLib? Is it possible to decide which logger I would like to use?

Thanks.

jruizaranguren commented 7 years ago

The problem is generated by LibLog library. It can be circumvented at least in two different ways:

PaulParau commented 7 years ago

You are correct, Metrics.NET uses LibLog to provide logging capabilities, which tries to load supported logging frameworks automatically through reflection. The workarounds you mentioned should work, but aren't ideal.

Looking throught the LibLog code, I noticed that each logger provider implementation has a ProviderIsAvailableOverride property, which, if set to false, should disable the particular provider. Unfortunately, it seems that the specific ILogProvider implementations (e.g. EntLibLogProvider) are marked as internal, so you can't set this property in your project.

Making the providers public and allowing Metrics.NET users to disable unwanted providers seems like a better alternative then the workarounds you mentioned (at first glance anyways), but I have to look a bit more at the code to see if making them public wouldn't have any undesired side-effects.