Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force nlog to throw an exception when the logging to database fails?

Tags:

nlog

When I take down the database that backs nlog, nothing gets get logged and it seems NLog swallows the problem. Is there any way to configure it to raise and exception or at least to log in a text file that logging failed?

Here is what my configuration looks like:

<?xml version="1.0" ?>
<nlog autoReload="true" throwExceptions="true" internalLogFile="${basedir}/App_Data/nlog.txt" internalLogLevel="Debug"
 internalLogToConsole="true">

 <targets>
 <!--Useful for debugging-->
 <target name="consolelog" type="ColoredConsole"
 layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" />



 <target name="databaselog" type="Database">

 <dbProvider>System.Data.SqlClient</dbProvider>

 <!-- database connection parameters -->
 <!-- alternatively you could provide a single 'connectionstring' parameter -->
 <connectionString>Data Source=.\SQLEXPRESSZ;Initial Catalog=aspnetdb;Integrated Security=SSPI</connectionString>

 <commandText>
 insert into NLog_Error ([time_stamp],[level],[host],[type],[source],[logger],[message],[stacktrace],[allxml]) values(@time_stamp,@level,@host,@type,@source,@logger,@message,@stacktrace,@allxml);
 </commandText>

 <parameter name="@time_stamp" layout="${utc_date}" />
 <parameter name="@level" layout="${level}" />
 <parameter name="@host" layout="${machinename}" />
 <parameter name="@type" layout="${exception:format=type}" />
 <parameter name="@source" layout="${callsite:className=true:fileName=false:includeSourcePath=false:methodName=false}" />
 <parameter name="@logger" layout="${logger}" />
 <parameter name="@message" layout="${message}" />
 <parameter name="@stacktrace" layout="${exception:stacktrace}" />
 <parameter name="@allxml" layout="${web_variables}" />

 </target>

 </targets>

 <rules>

 <logger name="*" minlevel="Info" writeTo="databaselog" />
 </rules>

</nlog>
like image 578
m0s Avatar asked Feb 07 '12 20:02

m0s


People also ask

Which of the following logging levels does NLog support?

NLog supports the following levels: Trace - Very detailed log messages, potentially of a high frequency and volume. Debug -Less detailed and/or less frequent debugging messages. Info - Informational messages.

How do I store log in database using NLog?

To start logging, we need to create a Logger instance. Before creating the Logger instance, we are configuring Nlog by passing the configuration file nlog. config which we are going to create in the next section. The GetCurrentClassLogger() method returns the Logger instance with the name of the current class (Nlog.


2 Answers

You can force Nlog to throw exception when sql server is not reached by following

<nlog throwExceptions="true">
 ... your nlog config
</nlog>

More info here,

http://nlog-project.org/2010/09/05/new-exception-handling-rules-in-nlog-2-0.html

It's a new feature in v2.0 so you need v2.0.

It will not work in earlier versions.

Also checkout following configuration info

https://github.com/NLog/NLog/wiki/Logging-Troubleshooting

which allows Nlog to log it's own exceptions to a specified file.

like image 94
N30 Avatar answered Oct 06 '22 00:10

N30


  1. Does NLog.config have the property "Copy to Output Directory" set as "Copy always"?
  2. I think you have wrong NLog.config file: you use elements instead of attributes within the target (documentation). Should be something like this:
<target 
  name="databaselog" 
  type="Database"
  dbProvider="System.Data.SqlClient"
  connectionString="Data Source=.\SQLEXPRESSZ;Initial Catalog=aspnetdb;Integrated Security=SSPI"
  commandText="insert into NLog_Error ([time_stamp],[level],[host],[type],[source],[logger],[message],[stacktrace],[allxml]) values(@time_stamp,@level,@host,@type,@source,@logger,@message,@stacktrace,@allxml);">
    <parameter name="@time_stamp" layout="${utc_date}" />
    <parameter name="@level" layout="${level}" />
    <parameter name="@host" layout="${machinename}" />
    <parameter name="@type" layout="${exception:format=type}" />
    <parameter name="@source" layout="${callsite:className=true:fileName=false:includeSourcePath=false:methodName=false}" />
    <parameter name="@logger" layout="${logger}" />
    <parameter name="@message" layout="${message}" />
    <parameter name="@stacktrace" layout="${exception:stacktrace}" />
    <parameter name="@allxml" layout="${web_variables}" />
</target>
like image 39
kolbasov Avatar answered Oct 06 '22 01:10

kolbasov