Target database not working with NLog 3.1 and Windsor Castle Logging Facility

你离开我真会死。 提交于 2019-12-11 02:04:42

问题


I'm able to log to a file using NLog, but the database is not created and if I create it manually, doesn't log anything.

Is there something I might be missing?

This is my NLog.config

<?xml version="1.0" encoding="utf-8" ?>

<target name="database" xsi:type="Database">
  <connectionString>
    Server=.\SQLEXPRESS; Database=Logging; User Id=user; Password=password;
  </connectionString>
  <commandText>
    insert into LogEntries(Date,Level,Logger,Message,MachineName, UserName, Callsite, ThreadId, Exception, InnerException, Stacktrace, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) values(@time_stamp, @level, @logger, @message,@machinename, @user_name, @call_site, @threadid, @log_exception, @log_innerexception, @stacktrace, @ErrorSource, @ErrorClass, @ErrorMethod, @ErrorMessage, @InnerErrorMessage);
  </commandText>
  <parameter name="@time_stamp" layout="${longdate}"/>
  <parameter name="@level" layout="${level:uppercase=true}"/>
  <parameter name="@logger" layout="${logger}"/>
  <parameter name="@message" layout="${message}"/>
  <parameter name="@machinename" layout="${machinename}"/>
  <parameter name="@user_name" layout="${windows-identity:domain=true}"/>
  <parameter name="@call_site" layout="${callsite:filename=true}"/>
  <parameter name="@threadid" layout="${threadid}"/>
  <parameter name="@ErrorSource" layout="${event-context:item=error-source}" />
  <parameter name="@ErrorClass" layout="${event-context:item=error-class}" />
  <parameter name="@ErrorMethod" layout="${event-context:item=error-method}" />
  <parameter name="@ErrorMessage" layout="${event-context:item=error-message}" />
  <parameter name="@InnerErrorMessage" layout="${event-context:item=inner-error-message}" />
  <parameter name="@log_exception" layout="${exception:format=type,message,method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}"/>
  <parameter name="@log_innerexception" layout="${exception:method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}"/>
  <parameter name="@stacktrace" layout="${stacktrace}"/>
  <dbProvider>System.Data.SqlClient</dbProvider>
  <install-command>
    <text>CREATE DATABASE Logging</text>
    <connectionString>Server=.\SQLEXPRESS; Database=master; User Id=user; Password=password;</connectionString>
    <ignoreFailures>true</ignoreFailures>
  </install-command>

  <install-command>
    <text>
      CREATE TABLE [dbo].[LogEntries](
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [Level] [varchar](5) NOT NULL,
      [Message] [varchar](4095) NOT NULL,
      [Date] [datetime] NOT NULL,
      [Logger] [varchar](200) NULL,
      [MachineName] [varchar](100) NULL,
      [UserName] [varchar](100) NULL,
      [CallSite] [varchar](100) NULL,
      [ThreadId] [varchar](100) NULL,
      [Exception] [varchar](max) NULL,
      [InnerException] [varchar](max) NULL,
      [Stacktrace] [varchar](max) NULL,
      ErrorSource [varchar](max) NULL, 
      ErrorClass [varchar](max) NULL, 
      ErrorMethod [varchar](max) NULL, 
      ErrorMessage [varchar](max) NULL, 
      InnerErrorMessage [varchar](max) NULL,
      CONSTRAINT [PK_Logs] 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]
    </text>
  </install-command>

   <!--commands to uninstall database--> 
  <uninstall-command>
    <text>DROP DATABASE Logging</text>
    <connectionString>Server=.\SQLEXPRESS; Database=master; User Id=user; Password=user;</connectionString>
    <ignoreFailures>true</ignoreFailures>
  </uninstall-command>
</target>


回答1:


If you have any kind of troubles with Nlog it is always a good starting point to turn on the internal logging with adding this to your nlog.config's root element:

<nlog internalLogFile="c:\log.txt" internalLogLevel="Trace">

In your concrete configuration the install-command are not run automatically because NLOG does not run them by itself.

You need to use a command line tool called InstallNLogConfig.exe which runs these commands with calling:

InstallNLogConfig.exe c:\path\to\NLog.config

If you have installed NLog with nuget you can find this command in

\packages\NLog.{version}\tools\{yourFrameworkVersion}\InstallNLogConfig.exe

You can read more about the here: Deploying NLog configuration files



来源:https://stackoverflow.com/questions/27592291/target-database-not-working-with-nlog-3-1-and-windsor-castle-logging-facility

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!