我有在项目使用.NET MVC 5时设置的NLog配置。在我将此代码移植到ASP.NET Core 2.1之前,它一直用于正确记录。现在它根本不会记录任何内容。
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);
}
}
当我要将任何条目记录到数据库时,将引用我的控制器。有关其引用方式的示例如下:
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”);
这曾经在.NET MVC 5中工作,但是当我移植代码时,它停止工作。我所做的唯一更改是在LoggingSettings文件中:
var dbTarget = new DatabaseTarget();
dbTarget.ConnectionString = DatabaseGlobals.ConnectionString;
它曾经是
var dbTarget = new DatabaseTarget();
dbTarget.ConnectionStringName = "App";
但是我认为ConnectionStringName被淘汰了吗?
我尝试调试,但是没有出现错误。
答案 0 :(得分:0)
与其在Controller中设置日志,不如考虑遵循Wiki:https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2
确保已安装nuget-package System.Data.SqlClient
以便在NetCore上使用DatabaseTarget。
确保已检查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。