Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get NLog to write to database

I'm trying to get NLog to log to my database log table but to no avail. I'm sure my connection string is correct because it's the same used elsewhere in my web.config. Writing out to a file works fine, so I know it's not just NLog, but must be something I'm doing wrong. Below is my NLog configuration:

<!-- NLOG CONFIGURATION -->   <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">     <targets>       <target name="file" xsi:type="File" fileName="${basedir}/logs/Log ${shortdate}.txt" layout="${longdate} ${callsite} ${level}: ${message} ${exception:format=Message,StackTrace} ${stacktrace}" />       <target type="Database" name="database" connectionstring="MyConnectionString">         <commandText>           insert into MyLog ([CreateDate], [Origin], [LogLevel], [Message], [Exception], [StackTrace]) values (@createDate, @origin, @logLevel, @message, @exception, @stackTrace);         </commandText>         <parameter name="@createDate" layout="${longdate}"/>         <parameter name="@origin" layout="${callsite}"/>         <parameter name="@logLevel" layout="${level}"/>         <parameter name="@message" layout="${message}"/>         <parameter name="@exception" layout="${exception:format=Message,StackTrace}"/>         <parameter name="@stackTrace" layout="${stacktrace}"/>       </target>     </targets>     <rules>       <logger name="*" writeTo="file"/>       <logger name="*" appendTo="database"/>       <!--<logger name="*" writeTo="mail" minlevel="Error"/>-->     </rules>   </nlog> 
like image 841
Ciaran O'Neill Avatar asked Jun 17 '09 16:06

Ciaran O'Neill


People also ask

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.

What is NLog writing?

NLog is a flexible and free logging platform for various . NET platforms, including . NET standard. NLog makes it easy to write to several targets. (database, file, console) and change the logging configuration on-the-fly.


1 Answers

Try putting the following in your nlog tag:

<nlog throwExceptions="true" internalLogFile="c:\nlog.txt" internalLogLevel="Debug" /> 

That might help determine what the problem is

like image 86
slolife Avatar answered Oct 11 '22 19:10

slolife