ASP.NET Core 2.1使用Nlog配置作为.NET MVC5 [英] ASP.NET Core 2.1 Using Nlog configuration as .NET MVC5

查看:104
本文介绍了ASP.NET Core 2.1使用Nlog配置作为.NET MVC5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在项目使用.NET MVC 5时设置的NLog配置.在我将此代码移植到ASP.NET Core 2.1之前,它一直用于正确记录.现在,它根本不会记录任何内容.

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

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

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

App.Data > ApplicationUsersData

这是我初始化的方式

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

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

这就是我所说的:

logger.Info("Test");

logger.Info("Test");

这曾经在.NET MVC 5中起作用,但是当我将代码移植过来时,它停止了工作.我所做的唯一更改是在LoggingSettings文件中:

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;

它曾经是

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

但是我认为ConnectionStringName被淘汰了吗?

But I think ConnectionStringName was phased out?

我尝试调试,但是没有出现错误.

I have tried debugging but no errors appeared.

推荐答案

与其在Controller中设置日志,不如考虑遵循以下Wiki:

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

确保已安装nuget-package System.Data.SqlClient以便在NetCore上使用DatabaseTarget.

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

确保已检查NLog InternalLogger: https://github.com /NLog/NLog/wiki/内部记录

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

NLog.Web.AspNetCore版本. 4.8.0允许您使用${configsetting}从appsettings.json中分配ConnectionString: https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer .

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.

这篇关于ASP.NET Core 2.1使用Nlog配置作为.NET MVC5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆