gpt4 book ai didi

c# - NLog 控制台应用程序

转载 作者:行者123 更新时间:2023-11-30 16:04:29 30 4
gpt4 key购买 nike

我有一个控制台应用程序,为了记录它的事件,我复制了我在 Web 应用程序中拥有的相同 Nlog.config 文件。我还导入了 Nlog.ExtendedNlog.Web。我还检查了 Nlog.config 属性(Build Action = Content,复制到输出目录 = copy Always)并设置了 internalLogFile 和 internalLogLevel。

什么时候

private static Logger logger = LogManager.GetCurrentClassLogger();

被称为 'System.TypeInitializationException' 被抛出,毕竟

Scanning XXXXLayoutRenderer (all parameter in my config)

这是比较我的内部日志文件

Info Found 105 configuration items

没有别的了

这是我的NLog.config文件

  <?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
internalLogFile="c:\Temp\internal_log_file.txt"
internalLogLevel="Trace"
internalLogToConsole="true">


<variable name="brief" value="${longdate} | ${level} | ${message}"/>
<variable name="verbose" value="${longdate} | ${machinename} | ${processid} | ${processname} | ${level} | ${logger} | ${message}"/>
<variable name="logDirectory" value="${basedir}/Logs/${date:format=yyyyMM}"/>

<targets>


<!-- FILE target -->
<target name="file" xsi:type="File" layout="${brief}" fileName="${logDirectory}/${shortdate}.txt"/>

<!-- EMAIL target -->
<target name="email" xsi:type="Mail"
smtpServer="smtp.gmail.com"
smtpPort="587"
smtpAuthentication="Basic"
smtpUserName="user@gmail.com"
smtpPassword="password"
enableSsl="true"
from="user@gmail.com"
to="user@gmail.com"
layout="${verbose}"
/>

<!-- DATABASE target -->
<target name="database" xsi:type="Database">


<connectionStringName>NLogEntities</connectionStringName>

<commandText>
insert into dbo.Log (
Application, Logged, Level, Message,
Username,
ServerName, Port, Url, Https,
ServerAddress, RemoteAddress,
Logger, CallSite, Exception
) values (
@Application, @Logged, @Level, @Message,
@Username,
@ServerName, @Port, @Url, @Https,
@ServerAddress, @RemoteAddress,
@Logger, @Callsite, @Exception
);
</commandText>
<parameter name="@application" layout="${appsetting:key=AppName:default=Unknown}" />
<parameter name="@logged" layout="${date}" />
<parameter name="@level" layout="${level}" />
<parameter name="@message" layout="${message}" />

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

<parameter name="@serverName" layout="${aspnet-request:serverVariable=SERVER_NAME:default=Unknown}" />
<parameter name="@port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
<parameter name="@url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
<parameter name="@https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />

<parameter name="@serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
<parameter name="@remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />

<parameter name="@logger" layout="${logger}" />
<parameter name="@callSite" layout="${callsite}" />
<parameter name="@exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<!-- DATABASE rules -->
<logger name="*" minlevel="Error" writeTo="database" />
<!-- EMAIL rules -->
<logger name="*" minlevel="Error" writeTo="email" />
<!-- FILE rules -->
<logger name="*" minlevel="Trace" writeTo="file" />
</rules>
</nlog>

这是输出内部日志文件

    2016-01-22 12:47:43.0520 Debug ScanAssembly('NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')
....
....

2016-01-22 12:47:43.2532 Trace Scanning LongDateLayoutRenderer 'Layout Renderer: ${longdate}'
2016-01-22 12:47:43.2532 Trace Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2016-01-22 12:47:43.2532 Trace Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2016-01-22 12:47:43.2532 Trace Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2016-01-22 12:47:43.2532 Trace Scanning MessageLayoutRenderer 'Layout Renderer: ${message}'
2016-01-22 12:47:43.2532 Info Found 105 configuration items

问题出在哪里?

最佳答案

已找到解决方案!!!我不得不改变这个

<parameter name="@application" layout="${appsetting:key=AppName:default=Unknown}" />

有了这个

<parameter name="@application" layout="${appsetting:name=AppName:default=Unknown}" />

key 词必须替换为 name 词。

已解决。

关于c# - NLog 控制台应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34946128/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com