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

末鹿安然 提交于 2019-12-25 01:25:58

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/52584564/asp-net-core-2-1-using-nlog-configuration-as-net-mvc5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!