【问题标题】:Nlog System.ArgumentException: LayoutRenderer cannot be found:Nlog System.ArgumentException:找不到 LayoutRenderer:
【发布时间】:2017-12-20 16:08:23
【问题描述】:

当我想将 asp.net 相关信息呈现到我的数据库中时,我在使用 NLog 时遇到了问题。这是 NLog 的内部日志记录。

2017-07-16 03:08:06.5485 Debug ScanAssembly('NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=')
2017-07-16 03:08:06.5796 Debug Start auto loading, location: 
2017-07-16 03:08:06.5796 Debug Auto loading done
2017-07-16 03:08:06.6036 Error Error parsing layout aspnet-request-method will be ignored. Exception: System.ArgumentException: LayoutRenderer cannot be found: 'aspnet-request-method'. Is NLog.Web not included?
   at NLog.Config.Factory`2.CreateInstance(String name)
   at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr)
2017-07-16 03:08:06.6115 Error Error parsing layout aspnet-request-method will be ignored. Exception: System.ArgumentException: LayoutRenderer cannot be found: 'aspnet-request-method'. Is NLog.Web not included?
   at NLog.Config.Factory`2.CreateInstance(String name)
   at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr)
2017-07-16 03:08:06.6115 Debug --- NLog configuration dump ---
2017-07-16 03:08:06.6115 Debug Targets:
2017-07-16 03:08:06.6276 Debug Rules:
2017-07-16 03:08:06.6276 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] appendTo: [  ]
2017-07-16 03:08:06.6276 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] appendTo: [  ]
2017-07-16 03:08:06.6276 Debug --- End of NLog configuration dump ---
2017-07-16 03:08:06.6456 Info Found 24 configuration items
2017-07-16 03:08:06.6966 Info Found 24 configuration items
2017-07-16 03:08:06.7085 Debug Targets for LicenseServer.Utility.Log by level:
2017-07-16 03:08:06.7085 Debug Trace =>
2017-07-16 03:08:06.7085 Debug Debug =>  
2017-07-16 03:08:06.7225 Debug Info =>  
2017-07-16 03:08:06.7225 Debug Warn =>  
2017-07-16 03:08:06.7225 Debug Error =>  
2017-07-16 03:08:06.7225 Debug Fatal =>  

我正在避免对 xml 文件进行配置。所以我采取了程序化配置的方法。这完全可以正常工作,除了当我放置“aspnet-request-method”和任何其他与 aspnet 相关的布局时它会出现问题。我安装了 NLog.Web 和 NLog.Extend 但它仍然无法正常工作。以下是我设置配置的代码:

public static class Log
{
    public static Logger _logger { get; set; }

    public static void Init()
    {

        InternalLogger.LogFile = @"D:\Nlog.txt";
        InternalLogger.LogLevel = LogLevel.Debug;
        var config = new LoggingConfiguration();
        var db_target = new DatabaseTarget();
        db_target.DBProvider = @"MySql.Data.MySqlClient.MySqlConnection, MySql.Data";
        db_target.ConnectionString =
            $@"server={DBConfig._server};database={DBConfig._dbname};userid={DBConfig._username};password={DBConfig
                ._password};";
        db_target.DBHost = DBConfig._server;
        db_target.DBDatabase = DBConfig._dbname;
        db_target.DBUserName = DBConfig._username;
        db_target.DBDatabase = DBConfig._password;
        db_target.CommandText = @"INSERT INTO error_log(system, module, action, content) VALUES(@activityid, @request, @logger, @ip)";
        db_target.Parameters.Add(new DatabaseParameterInfo("@activityid", new NLog.Layouts.SimpleLayout("${activityid}")));
        db_target.Parameters.Add(new DatabaseParameterInfo("@request", new NLog.Layouts.SimpleLayout(@"${aspnet-request-method}")));
        db_target.Parameters.Add(new DatabaseParameterInfo("@logger", new NLog.Layouts.SimpleLayout("${logger}")));
        db_target.Parameters.Add(new DatabaseParameterInfo("@ip", new NLog.Layouts.SimpleLayout("${aspnet-request-method}")));
        db_target.KeepConnection = true;
        var rule = new LoggingRule("*", LogLevel.Debug, db_target);
        config.LoggingRules.Add(rule);
        config.LoggingRules.Add(rule);
        LogManager.Configuration = config;
        _logger = LogManager.GetCurrentClassLogger();
    }
}

【问题讨论】:

标签: asp.net .net logging nlog


【解决方案1】:

请注意,您需要在 nlog.config 中安装 NLog.Web(ASP.NET 非核心)或 NLog.Web.AspNetCore,对于 ASP.NET Core,您需要安装

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    相关资源
    最近更新 更多