bugthesystem / Autofac.Extras.NLog

An Autofac module to integrate Autofac and NLog, it supports both constructor and property injection.
MIT License
50 stars 18 forks source link

Write Database #22

Closed armory09 closed 5 years ago

armory09 commented 5 years ago

Hi, I was trying to setup my project ASP.NET MVC with .Net Framework 4.7.2 to use NLog and Autofac. It just strange that , when writing a log to a file it was working as expected, but when trying to log in database it doesn't log anything.

Here is my config in DI:

 // Register NLog
 builder.RegisterModule<NLogModule>();
builder.RegisterType<LoggerFactory>().As<ILoggerFactory>().SingleInstance();
builder.RegisterGeneric(typeof(Logger<>)).As(typeof(ILogger<>)).SingleInstance();

var container = builder.Build();

var loggerFactory = container.Resolve<ILoggerFactory>();

loggerFactory.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true });

NLog.config:

<?xml version="1.0" encoding="utf-8" ?>
<nlog
  xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
  throwConfigExceptions="true">
  <extensions>
    <add assembly="NLog.Extended" />
  </extensions>
  <targets>
    <!-- database target -->
    <target name="database"
     xsi:type="Database"
     connectionStringName="ExceptionLogConnectionString"
     commandType="StoredProcedure"
     commandText="[dbo].[InsertLog]">
      <parameter name="@level" layout="${level}" />
      <parameter name="@callSite" layout="${callsite}" />
      <parameter name="@type" layout="${exception:format=type}" />
      <parameter name="@message" layout="${exception:format=message}" />
      <parameter name="@stackTrace" layout="${exception:format=stackTrace}" />
      <parameter name="@innerException"
                  layout="${exception:format=:innerFormat=ShortType,Message,Method:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}" />
      <parameter name="@additionalInfo" layout="${message}" />
    </target>
  </targets>

  <rules>
    <!-- database logger -->
    <logger levels="Error,Warn,Fatal" name="databaseLogger" writeTo="database"/>
  </rules>
</nlog>

Im trying to instantiate it like this inside the controller:

public class TestController {
private readonly ILogger _logger;

    public TestController(ILogger logger){
         _logger = logger;
    }
}

or like this:

public class TestController {
private readonly ILogger<TestController> _logger;

    public TestController(ILogger<TestController> logger){
         _logger = logger;
    }
}

But still doesn't log anything, Can anyone please guide me?

PS:

This code was able to insert log in database, but not the above setup

// get a Logger object and log exception here using NLog. 
// this will use the "databaseLogger" logger from our NLog.config file
NLog.Logger logger = NLog.LogManager.GetLogger("databaseLogger");

// add custom message and pass in the exception
logger.Error(ex, "Whoops!");

Thanks, Jonathan

armory09 commented 5 years ago

Ok the issue to this one is the rule:

<logger levels="Error,Warn,Fatal" name="databaseLogger" writeTo="database"/>

change the name value to * for more details here is the Stackoverflow link