简体   繁体   中英

ASP.NET Core 2.1 Using Nlog configuration as .NET MVC5

I have this NLog configuration that was set up when my project was using .NET MVC 5. It used to log properly until I ported this code over to ASP.NET Core 2.1. Now it won't log anything at all.

App.ClassLibrary > LoggingSettings.cs

public class LoggingSettings
{
    public static void ConfigureLogger()
    {
        var logConfig = new LoggingConfiguration();
        var consoleTarget = new ColoredConsoleTarget();
        consoleTarget.Layout = consoleTarget.Layout = @"${date:format=HH\:mm\:ss} ${logger} ${message} ${mdc:item=AppName} ${mdc:item=UserName}";
        logConfig.AddTarget("console", consoleTarget);

        var dbTarget = new DatabaseTarget();
        dbTarget.ConnectionString = DatabaseGlobals.ConnectionString;

        dbTarget.CommandText = @"
            INSERT INTO [dbo].[AspNetEventLogs]
            ([Application]
            ,[Logged]
            ,[Level]
            ,[Message]
            ,[UserName]
            ,[ServerName]
            ,[Port]
            ,[Url]
            ,[Https]
            ,[ServerAddress]
            ,[Logger]
            ,[Callsite]
            ,[Exception])
                VALUES (
            @application,
            @logged,
            @level,
            @message,
            @UserName,
            @serverName,
            @port,
            @url,
            @https,
            @serverAddress,
            @logger,
            @callSite,
            @exception
            )
        ";
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@Application", "${mdc:item=AppName}"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@Logged", "${date}"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@Level", "${level}"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@Message", "${message}"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@Username", "${identity}"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@ServerName", "n/a"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@Port", "n/a"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@Url", "n/a"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@Https", "0"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@ServerAddress", "n/a"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@Logger", "${logger}"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@CallSite", "${callsite}"));
        dbTarget.Parameters.Add(new DatabaseParameterInfo("@Exception", "${exception:tostring"));

        var rule = new LoggingRule("*", LogLevel.Debug, dbTarget);
        logConfig.LoggingRules.Add(rule);

        LogManager.Configuration = logConfig;
    }
}

App.ClassLibrary > LoggingController.cs

public class LoggingController : Logger
{
    public void ConfigureLogger()
    {
        LoggingSettings.ConfigureLogger();
    }

    public void SetApplicationName(String Name)
    {
        MappedDiagnosticsContext.Set("AppName", Name);
    }
}

My controller is then referred to when I want to log any entries to the database. An example of how its referenced is here:

App.Data > ApplicationUsersData

This is how I initialise it

private readonly LoggingController logger = (LoggingController)LogManager.GetCurrentClassLogger(typeof(LoggingController));

public ApplicationUsersData(ApplicationDbContext dbContext)
{
    this.dbContext = dbContext;
    logger.ConfigureLogger();
    logger.SetApplicationName(ConfigurationManager.AppSettings["AppName"]);
}

This is how I call it:

logger.Info("Test");

This used to work in .NET MVC 5 but when I ported the code over, it stopped working. The only change I made was in the LoggingSettings file:

var dbTarget = new DatabaseTarget();
dbTarget.ConnectionString = DatabaseGlobals.ConnectionString;

It used to be

var dbTarget = new DatabaseTarget();
dbTarget.ConnectionStringName = "App";

But I think ConnectionStringName was phased out?

I have tried debugging but no errors appeared.

Instead of setting up logging in the Controller then consider following the wiki: https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2

Make sure that you have installed nuget-package System.Data.SqlClient to use DatabaseTarget on NetCore.

Make sure that you have checked the NLog InternalLogger: https://github.com/NLog/NLog/wiki/Internal-Logging

NLog.Web.AspNetCore ver. 4.8.0 allows you to use the ${configsetting} to assign ConnectionString from appsettings.json: https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM