目标数据库不适用于NLog 3.1和Windsor Castle日志记录功能 [英] Target database not working with NLog 3.1 and Windsor Castle Logging Facility

查看:76
本文介绍了目标数据库不适用于NLog 3.1和Windsor Castle日志记录功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用NLog登录到文件,但是不会创建数据库,并且如果我手动创建它,则不会记录任何内容.

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?

这是我的NLog.config

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>

推荐答案

如果您对Nlog遇到任何麻烦,总是打开

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">

在您的具体配置中,install-command不会自动运行,因为NLOG不会单独运行它们.

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

您需要使用名为InstallNLogConfig.exe的命令行工具,该工具通过调用来运行以下命令:

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

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

如果已使用nuget安装了NLog,则可以在

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

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

您可以在此处了解更多信息:部署NLog配置文件

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

这篇关于目标数据库不适用于NLog 3.1和Windsor Castle日志记录功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆