這個問題可以通過配置日誌目標代碼迎刃而解:
var target = CreateDatabaseTarget();
LogManager.Configuration.AddTarget("databaseTarget", CreateDatabaseTarget());
LogManager.Configuration.LoggingRules.Add(new NLog.Config.LoggingRule("*", LogLevel.Warn, target));
,如果你是罰款,但:
private DatabaseTarget CreateDatabaseTarget()
{
var entityFrameworkConnection = ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString;
var builder = new EntityConnectionStringBuilder(entityFrameworkConnection);
var connectionString = builder.ProviderConnectionString;
var target = new DatabaseTarget()
{
ConnectionString = connectionString,
CommandText = @"insert into Log ([DateTime], [Message]) values (@dateTime, @message);",
Parameters = {
new DatabaseParameterInfo("@dateTime", new NLog.Layouts.SimpleLayout("${date}")),
new DatabaseParameterInfo("@message", new NLog.Layouts.SimpleLayout("${message}")),
}
};
return target;
}
然後你可以用你的NLOG配置寄存器採取一些Nuget的依賴或想要一個更完整的解決方案,你可以看看NLog.Mvc和NLog.EntityFramework存儲庫誰都可以nuget包...
應該指出,OP是您鏈接到的NLog.EntityFramework軟件包的創建者。 – 2015-01-24 11:22:03
是的,這個問題的結果是當時沒有滿意的答案。 :-) – ladenedge 2015-09-29 16:16:49