Reputation: 3024
I've tried to implement NLog in my application. However, for some reason nothing is being written to the database. I have tried to debug a simple info and noticed that I have this error :
- DeclaringMethod '((System.RuntimeType)log._loggerType).DeclaringMethod' threw an exception of type 'System.InvalidOperationException' System.Reflection.MethodBase {System.InvalidOperationException}
Not sure how this is relevant.
I have switched on internal debugging as well and have not received any noticeable errors. This is the internal debug log:
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 =>
Here is my nlog.config file. I have created the database table based on what this config shows:
<?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>
Upvotes: 0
Views: 717
Reputation: 19847
The name
of the logger-rules is actually a wildcard for matching logger-names. Try changing to a star instead:
<rules>
<logger name="*" minlevel="Debug" writeTo="database" />
<rules>
See also https://github.com/NLog/NLog/wiki/Configuration-file#rules
Upvotes: 1