我(我认为是)NLog的一个非常简单的数据库记录器,没有任何异国情调或令人兴奋,但是我被大吼大叫而且我没有任何关于问题是什么的信息。
我把NLog连接到Common.Logging就像这样:
<common>
<logging>
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog40">
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
</common>
<nlog
internalLogLevel="Trace"
internalLogFile="c:\logs\myservice.api.nlog.log"
throwExceptions="true">
<extensions>
<add assembly="NLog.Extended" />
</extensions>
<targets>
<target
type="Database"
name="database"
dbProvider="mssql"
commandType="Text"
connectionStringName="MyServiceDbConnection"
commandText="INSERT INTO dbo.LOGS (Machine, ExecutingAssembly, UtcTimestamp, Source, Thread, Severity, Username, Message) VALUES (@Machine, @ExecutingAssembly, @UtcTimestamp, @Source, @Thread, @Severity, @Username, @Message);">
<parameter name="@Machine" layout="${machinename}" />
<parameter name="@ExecutingAssembly" layout="${literal:text=MyService.Api}" />
<parameter name="@UtcTimestamp" layout="${date:universalTime=true}" />
<parameter name="@Source" layout="${logger}" />
<parameter name="@Thread" layout="${threadname}" />
<parameter name="@Severity" layout="${level:uppercase=true}" />
<parameter name="@Username" layout="${aspnet-user-identity}" />
<parameter name="@Message" layout="${message} ${exception:format=ToString}" />
</target>
</targets>
<rules>
<logger name="*" minLevel="Debug" writeTo="database" />
</rules>
</nlog>
当我尝试运行我的api应用程序时,收到以下错误消息:
Server Error in '/' Application.
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: An error occurred creating the configuration section handler for nlog: Error when setting property 'Layout' on NLog.Targets.DatabaseParameterInfo
Source Error:
Line 86: </logging>
Line 87: </common>
Line 88: <nlog
Line 89: internalLogLevel="Trace"
Line 90: internalLogFile="c:\logs\myservice.api.nlog.log"
我的内部NLog日志不是很有帮助:
2015-08-13 08:21:05.1643 Info Loading assembly name: NLog.Extended
2015-08-13 08:21:05.1643 Debug ScanAssembly('NLog.Extended, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')
2015-08-13 08:21:05.1753 Debug Setting 'DatabaseTarget.name' to 'database'
2015-08-13 08:21:05.1753 Debug Setting 'DatabaseTarget.dbProvider' to 'mssql'
2015-08-13 08:21:05.1753 Debug Setting 'DatabaseTarget.commandType' to 'Text'
2015-08-13 08:21:05.1753 Debug Setting 'DatabaseTarget.connectionStringName' to 'MyServiceDbConnection'
2015-08-13 08:21:05.1753 Debug Setting 'DatabaseTarget.commandText' to 'INSERT INTO dbo.LOGS (Machine, ExecutingAssembly, UtcTimestamp, Source, Thread, Severity, Username, Message) VALUES (@Machine, @ExecutingAssembly, @UtcTimestamp, @Source, @Thread, @Severity, @Username, @Message);'
2015-08-13 08:21:05.1753 Debug Setting 'DatabaseParameterInfo.name' to '@Machine'
2015-08-13 08:21:05.1753 Debug Setting 'DatabaseParameterInfo.layout' to '${machinename}'
2015-08-13 08:21:05.1753 Trace FindReachableObject<NLog.Internal.IRenderable>:
2015-08-13 08:21:05.1753 Trace Scanning MachineNameLayoutRenderer 'Layout Renderer: ${machinename}'
2015-08-13 08:21:05.1753 Debug Setting 'DatabaseParameterInfo.name' to '@ExecutingAssembly'
2015-08-13 08:21:05.1892 Debug Setting 'DatabaseParameterInfo.layout' to '${literal:text=MyService.Api}'
2015-08-13 08:21:05.1892 Debug Setting 'LiteralLayoutRenderer.text' to 'Onlife.Globalization.Api'
2015-08-13 08:21:05.1892 Trace FindReachableObject<NLog.Internal.IRenderable>:
2015-08-13 08:21:05.1892 Trace Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2015-08-13 08:21:05.1892 Debug Setting 'DatabaseParameterInfo.name' to '@UtcTimestamp'
2015-08-13 08:21:05.1892 Debug Setting 'DatabaseParameterInfo.layout' to '${date:universalTime=true}'
2015-08-13 08:21:05.1892 Debug Setting 'DateLayoutRenderer.universalTime' to 'true'
2015-08-13 08:21:05.1892 Trace FindReachableObject<NLog.Internal.IRenderable>:
2015-08-13 08:21:05.1892 Trace Scanning DateLayoutRenderer 'Layout Renderer: ${date}'
2015-08-13 08:21:05.1892 Debug Setting 'DatabaseParameterInfo.name' to '@Source'
2015-08-13 08:21:05.1892 Debug Setting 'DatabaseParameterInfo.layout' to '${logger}'
2015-08-13 08:21:05.1892 Trace FindReachableObject<NLog.Internal.IRenderable>:
2015-08-13 08:21:05.1892 Trace Scanning LoggerNameLayoutRenderer 'Layout Renderer: ${logger}'
2015-08-13 08:21:05.1892 Debug Setting 'DatabaseParameterInfo.name' to '@Thread'
2015-08-13 08:21:05.1892 Debug Setting 'DatabaseParameterInfo.layout' to '${threadname}'
2015-08-13 08:21:05.1892 Trace FindReachableObject<NLog.Internal.IRenderable>:
2015-08-13 08:21:05.1892 Trace Scanning ThreadNameLayoutRenderer 'Layout Renderer: ${threadname}'
2015-08-13 08:21:05.1892 Debug Setting 'DatabaseParameterInfo.name' to '@Severity'
2015-08-13 08:21:05.1892 Debug Setting 'DatabaseParameterInfo.layout' to '${level:uppercase=true}'
2015-08-13 08:21:05.1892 Debug Setting 'UppercaseLayoutRendererWrapper.uppercase' to 'true'
2015-08-13 08:21:05.1892 Trace Wrapping LevelLayoutRenderer with UppercaseLayoutRendererWrapper
2015-08-13 08:21:05.1892 Trace FindReachableObject<NLog.Internal.IRenderable>:
2015-08-13 08:21:05.1892 Trace Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2015-08-13 08:21:05.1892 Trace FindReachableObject<NLog.Internal.IRenderable>:
2015-08-13 08:21:05.1892 Trace Scanning UppercaseLayoutRendererWrapper 'Layout Renderer: ${uppercase}'
2015-08-13 08:21:05.1892 Trace Scanning SimpleLayout ''''
2015-08-13 08:21:05.1892 Trace Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2015-08-13 08:21:05.1892 Debug Setting 'DatabaseParameterInfo.name' to '@Username'
2015-08-13 08:21:05.1892 Debug Setting 'DatabaseParameterInfo.layout' to '${aspnet-user-identity}'
2015-08-13 08:21:15.5407 Info Shutting down logging...
2015-08-13 08:21:15.5407 Info Logger has been shut down.
没有其他错误消息或此处出现问题的迹象。我做错了什么?
答案 0 :(得分:8)
稍晚一点可能会有所帮助:
如果您使用的是NLog&gt; 4.0,ASP.NET渲染器在NLog.Web中。因此,您需要使用Nuget包添加它并引用它,如下所示:
<extensions>
<add assembly="NLog.Web" />
</extensions>
这就是全部,因为您正在使用以下参数:
<parameter name="@Username" layout="${aspnet-user-identity}" />
此处有更多信息:http://nlog-project.org/2015/06/13/NLog-Extended_NLog-Web_and_NLog-Windows-Forms.html