我能够使用NLog登录文件,但是没有创建数据库,如果我手动创建它,则不会记录任何内容。
我可能会遗漏一些东西吗?
这是我的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>
答案 0 :(得分:0)
如果您在使用Nlog时遇到任何问题,请将internal logging添加到您的nlog.config的根元素中,这是一个很好的起点:
<nlog internalLogFile="c:\log.txt" internalLogLevel="Trace">
在具体配置中,install-command
不会自动运行,因为NLOG不会自行运行它们。
您需要使用名为InstallNLogConfig.exe
的命令行工具,该工具通过调用:
InstallNLogConfig.exe c:\path\to\NLog.config
如果你已经用nuget安装了NLog,你可以在
中找到这个命令\packages\NLog.{version}\tools\{yourFrameworkVersion}\InstallNLogConfig.exe
您可以在此处详细了解:Deploying NLog configuration files