我无法使用NLog登录数据库。问题是什么?我正在使用ASP.NET Core 2.0 WebApi。
在Startup.cs上
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddNLog();
app.UseMvc();
app.AddNLogWeb();
var databaseTarget = new DatabaseTarget("DatabaseTarget");
databaseTarget.CommandText = "insert into dbo.Log (Message) values (@Message)";
databaseTarget.CommandType = System.Data.CommandType.Text;
databaseTarget.ConnectionString = @"server=.\SQLEXPRESS;database=CiPetPet;integrated security=true;";
databaseTarget.DBProvider = "sqlserver";
databaseTarget.Parameters.Add(new DatabaseParameterInfo("@Message", "${message}"));
LogManager.Configuration = new NLog.Config.LoggingConfiguration();
LogManager.Configuration.AddTarget("DatabaseTarget", databaseTarget);
LogManager.Configuration.LoggingRules.Add(new NLog.Config.LoggingRule("*", NLog.LogLevel.Trace, databaseTarget));
InternalLogger.LogFile = "InternalLog.txt";
}
在控制器上
[HttpGet]
public string Index()
{
LogManager.EnableLogging();
var logger = LogManager.GetCurrentClassLogger();
logger.Debug("Debug log");
logger.Error("Error log");
logger.Fatal("Fatal log");
logger.Info("Info log");
logger.Warn("Warn log");
return "Logging...";
}
内部日志文件正常工作。但是数据库日志不起作用。