来自文档https://github.com/NLog/NLog/wiki/Database-target 似乎设置显示为目标元素上的属性,例如:
<target xsi:type="Database"
name="String"
dbUserName="Layout"
dbProvider="String"
,并在下面的示例中作为单独的子节点:
<target name="database" xsi:type="Database">
<connectionStringName>NLogDb</connectionStringName>
这对我都不起作用,我仅收到此消息的无效配置异常:
NotSupportedException: Parameter connectionStringName not supported on DatabaseTarget
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="info"
throwExceptions="true"
internalLogFile="c:\temp\internal-nlog.txt">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<target xsi:type="Database"
name="database"
keepConnection="true"
useTransactions="true"
dbProvider="System.Data.SqlClient"
connectionStringName="DefaultConnection"
commandText="INSERT INTO Logs (EventDateTime, EventLevel, UserName, MachineName, EventMessage, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) VALUES (@EventDateTime, @EventLevel, @UserName, @MachineName, @EventMessage, @ErrorSource, @ErrorClass, @ErrorMethod, @ErrorMessage, @InnerErrorMessage)">
<parameter name="@EventDateTime" layout="${date:s}" />
<parameter name="@EventLevel" layout="${level}" />
<parameter name="@UserName" layout="${aspnet-user-identity}" />
<parameter name="@MachineName" layout="${machinename}" />
<parameter name="@EventMessage" layout="${message}" />
<parameter name="@ErrorSource" layout="${event-context:item=error-source}" />
<parameter name="@ErrorClass" layout="${event-context:item=error-class}" />
<parameter name="@ErrorMethod" layout="${event-context:item=error-method}" />
<parameter name="@ErrorMessage" layout="${event-context:item=error-message}" />
<parameter name="@InnerErrorMessage" layout="${event-context:item=inner-error-message}" />
</target>
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile,database" />
<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxLevel="Info" final="true
" />
<!-- BlackHole without writeTo -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
在program.cs中如何调用它
var logger = NLog.Web.NLogBuilder.ConfigureNLog( "nlog.config" ).GetCurrentClassLogger();
(从他们的文档中复制)
必须丢失一些明显的东西,但是由于文档中存在冲突的信息,并且复制了此处张贴的其他人的配置,因此不确定该在哪里使用
答案 0 :(得分:1)
好像您正在NetCore上运行。您发现自己已经发现NLog无法从AppSettings.json中读取connectionStringName
(需要额外的依赖项才能访问IConfiguration)。
一种可能的解决方案是使用此扩展程序:
https://www.nuget.org/packages/NLog.Appsettings.Standard/
并在NLog.config中使用connectionString
(而不是connectionStringName
)
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="NLog.Appsettings.Standard"/>
</extensions>
<target xsi:type="Database" connectionString="${appsettings:name=ConnectionStrings.DefaultConnection}">
另一种解决方案是在记录之前分配GDC变量:
NLog.GlobalDiagnosticsContext.Set("DefaultConnection", Configuration.GetConnectionString("DefaultConnection"));
然后在NLog.config中使用GDC:
<target xsi:type="Database" connectionString="${gdc:item=DefaultConnection}">
另请参阅https://github.com/NLog/NLog/wiki/Gdc-layout-renderer
更新NLog.Extension.Logging版本。 1.4.0
使用NLog.Extension.Logging版本。 1.4.0,那么您现在可以使用${configsetting}
另请参阅:https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer