Save log to database

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Save log to database

przemekpod
This post was updated on .
Hello, im using nlog in my asp mvc4 app(web_variables and utc_date are custom layouts ) and i struggle with configuration file:

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

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

    <target name="filelog" type="File" fileName="${basedir}/App_Data/Site.log"
     layout="${date}: ${message}" />

    <target name="eventlog" type="EventLog" source="My App" log="Application"
     layout="${date}: ${message} ${stacktrace}" />

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

      <dbProvider>sqlserver</dbProvider>

     
     
      <connectionString>"Data Source=DB;Initial Catalog=catalog;Persist Security Info=True;User ID=user;Password=Password;MultipleActiveResultSets=True;Application Name=EntityFramework</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="filelog" />
    <logger name="*" minlevel="Info" writeTo="databaselog" />
  </rules>

</nlog>


Could anyone tell me if thats correct config for saving logs to database and i got errors in my custom layouts or smth else?
With that config i got exception on creating Logger with innerexception msg: The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security.
With some standard config(to file) i dont get that exception

Edit:  db script for table:
CREATE TABLE [dbo].[NLog_Error](
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [time_stamp] [datetime] NOT NULL,
        [host] [nvarchar](max) NOT NULL,
        [type] [nvarchar](50) NOT NULL,
        [source] [nvarchar](50) NOT NULL,
        [message] [nvarchar](max) NOT NULL,
        [level] [nvarchar](50) NOT NULL,
        [logger] [nvarchar](50) NOT NULL,
        [stacktrace] [nvarchar](max) NOT NULL,
        [allxml] [ntext] NOT NULL,
 CONSTRAINT [PK_NLogError] PRIMARY KEY CLUSTERED
(
        [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [dbo].[NLog_Error] ADD  CONSTRAINT [DF_NLogError_time_stamp]  DEFAULT (getdate()) FOR [time_stamp]
GO

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

when i throw away from db tables almost all columns(except id and message) and change config:

    <commandText>
        insert into NLog_Error ([message]) values(@message);
      </commandText>

     
      <parameter name="@message" layout="${message}" />


i can create instnce of logger but when i do loger.info("sth") it dont go to data base, why?
Reply | Threaded
Open this post in threaded view
|

Re: Save log to database

przemekpod
OK i solved my problem by setting:
<nlog autoReload="true" throwExceptions="true" internalLogFile="c:\nlog.txt" internalLogLevel="Info"
      internalLogToConsole="true">
so i could get error log where i found that my connection stirng was wrong(it got ' " ' mark at start what is really bad)
Reply | Threaded
Open this post in threaded view
|

Re: Save log to database

jovball
In reply to this post by przemekpod
I'd suggest using the connectionStringName attribute and pointing to a named connectionString in your web.config/app.config file. Better for maintenance and only one place to make this kind of mistake.