My application is built on ASP .NET 5 I used to do a project on .NET Core and everything was OK, there were no problems with logs. But in ASP.NET 5 I do not understand how to do this I'm trying to write logs to the database using this config:
https://github.com/nlog/NLog/wiki/Database-target#example-configurations
This is my config file:
<?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="Off" 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>
<target name="database" xsi:type="Database">
<connectionStringName>"Server=localhost\\SQLEXPRESS;Database=LocalDB;User ID=sa;Password=1234;"</connectionStringName>
<commandText>
insert into dbo.LogExcelWorker (
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:default=Unknown\: set AppName in appSettings}" />
<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="*" minlevel="Debug" writeTo="database" />
</rules>
</nlog>
I created the table in the database
I try to make a test call of logs but nothing is written down:
public string Index()
{
Logger log = LogManager.GetCurrentClassLogger();
log.Trace( "trace message" );
log.Debug( "debug message" );
log.Info( "info message" );
log.Warn( "warn message" );
log.Error( "error message" );
log.Fatal( "fatal message" );
return "start...";
}
But there are no records in the database
connectionStringNamedoesn't look correct. It should just beconnectionStringwith no quotes around its value. - Rolf Kristensen