我尝试在我的应用程序中实现NLog。但是,由于某种原因,什么也没有写入数据库。我尝试调试一个简单的信息,并发现我有此错误:
- DeclaringMethod '((System.RuntimeType)log._loggerType).DeclaringMethod' threw an exception of type 'System.InvalidOperationException' System.Reflection.MethodBase {System.InvalidOperationException}
不确定这是如何关联的。
我也打开了内部调试,没有收到任何明显的错误。这是内部调试日志:
2018-07-23 00:25:37.1679 Trace Scanning Property Layout ''${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}'' NLog.Layouts
2018-07-23 00:25:37.1679 Trace Scanning SimpleLayout ''${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}''
2018-07-23 00:25:37.1868 Trace Scanning DatabaseParameterInfo 'NLog.Targets.DatabaseParameterInfo'
2018-07-23 00:25:37.1868 Trace Scanning Property Layout ''${logger}'' NLog.Layouts
2018-07-23 00:25:37.2058 Trace Scanning SimpleLayout ''${logger}''
2018-07-23 00:25:37.2148 Trace Scanning DatabaseParameterInfo 'NLog.Targets.DatabaseParameterInfo'
2018-07-23 00:25:37.2148 Trace Scanning Property Layout ''${callsite}'' NLog.Layouts
2018-07-23 00:25:37.2367 Trace Scanning SimpleLayout ''${callsite}''
2018-07-23 00:25:37.2457 Trace Scanning DatabaseParameterInfo 'NLog.Targets.DatabaseParameterInfo'
2018-07-23 00:25:37.2457 Trace Scanning Property Layout ''${exception:tostring}'' NLog.Layouts
2018-07-23 00:25:37.2687 Trace Scanning SimpleLayout ''${exception:tostring}''
2018-07-23 00:25:37.2887 Trace Database Target[database] has 16 layouts
2018-07-23 00:25:37.3007 Trace FindReachableObject<System.Object>:
2018-07-23 00:25:37.3117 Trace Scanning SimpleLayout ''Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-ManagementStudio-5cb5b1db-6c48-49c7-93b2-ba81ded39c1c;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ManagementStudio-5cb5b1db-6c48-49c7-93b2-ba81ded39c1c.mdf''
2018-07-23 00:25:37.3117 Trace Scanning Property Renderers 'System.Collections.ObjectModel.ReadOnlyCollection`1[NLog.LayoutRenderers.LayoutRenderer]' System.Collections.ObjectModel
2018-07-23 00:25:37.3296 Trace Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-07-23 00:25:37.4832 Debug Watching path 'C:\Users\amosa\Source\Repos\ManagementStudio\ManagementStudio' filter 'NLog.config' for changes.
2018-07-23 00:25:37.4936 Info Configuration initialized.
2018-07-23 00:25:37.5095 Info NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.5.7.7958. Product version: 4.5.7.
2018-07-23 00:25:37.5095 Debug Targets for ManagementStudio.Controllers.UsersController by level:
2018-07-23 00:25:37.5304 Debug Trace =>
2018-07-23 00:25:37.5398 Debug Debug =>
2018-07-23 00:25:37.5398 Debug Info =>
2018-07-23 00:25:37.5598 Debug Warn =>
2018-07-23 00:25:37.5768 Debug Error =>
2018-07-23 00:25:37.5768 Debug Fatal =>
这是我的nlog.config文件。我已根据此配置显示的内容创建了数据库表:
<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Trace" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
<target name="database" xsi:type="Database">
<connectionStringName>DefaultConnection</connectionStringName>
<commandText>
insert into dbo.AspNetEventLogs (
Application, Logged, Level, Message,
Username,
ServerName, Port, Url, Https,
ServerAddress, RemoteAddress,
Logger, CallSite, Exception
) values (
@Application, @Logged, @Level, @Message,
@Username,
@ServerName, @Port, @Url, @Https,
@ServerAddress, @RemoteAddress,
@Logger, @Callsite, @Exception
);
</commandText>
<parameter name="@application" layout="${appsetting:name=appName}" />
<parameter name="@logged" layout="${date}" />
<parameter name="@level" layout="${level}" />
<parameter name="@message" layout="${message}" />
<parameter name="@username" layout="${identity}" />
<parameter name="@serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />
<parameter name="@port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
<parameter name="@url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
<parameter name="@https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />
<parameter name="@serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
<parameter name="@remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />
<parameter name="@logger" layout="${logger}" />
<parameter name="@callSite" layout="${callsite}" />
<parameter name="@exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<logger name="database" minlevel="Debug" writeTo="database" />
<!-- add your logging rules here -->
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
</rules>
</nlog>
答案 0 :(得分:0)
记录器规则的name
实际上是用于匹配记录器名称的通配符。尝试改用星星:
<rules>
<logger name="*" minlevel="Debug" writeTo="database" />
<rules>
另请参阅https://github.com/NLog/NLog/wiki/Configuration-file#rules