gpt4 book ai didi

c# - NLog 拒绝抛出异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:55:10 24 4
gpt4 key购买 nike

您好亲爱的开发者,

我在使用 NLog 时遇到了一个奇怪的问题,
我遇到了崩溃,但无法在日志中找到用户事件的踪迹。

我假设,日志记录不起作用...
尝试搜索权限问题等,但似乎一切正常。

我想在出现问题时调试我的日志记录,所以我创建了以下内容
如果创建失败则告诉用户的代码:

public static class Log
{
static Log()
{
try
{
AppLogger = LogManager.GetLogger("Megatec.EngineeringMatrix.AppLogger");
ChangeLogger = LogManager.GetLogger("Megatec.EngineeringMatrix.ChangeLogger");
DRCLogger = LogManager.GetLogger("Megatec.EngineeringMatrix.DRCLogger");

if((AppLogger == null) || (ChangeLogger == null) || (DRCLogger == null))
throw new NLogConfigurationException("Configuration does not specify correct loggers");
writeStartupLogEntry();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), @"Failed to load `Megatec.EngineeringMatrix` Loggers.");
throw;
}

}

public static readonly Logger AppLogger;
public static readonly Logger ChangeLogger;
public static readonly Logger DRCLogger;

使用以下配置文件:

  <?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" throwExceptions="true">
<targets>
<target xsi:type="File" name="AppLogFile" createDirs="true" fileName="c:/log?s/${processname}/${logger}_${shortdate}.log"
layout=">> ${time} ${uppercase:${level}} ${callsite} ${message} ${exception:format=tostring}" />

<target xsi:type="File" name="ChangeLogFile" createDirs="true" fileName="c:/log?s/${processname}/${logger}_${shortdate}.log"
layout=">> ${date} ${message} ${exception:format=tostring}" />

<target xsi:type="File" name="DRCLogFile" createDirs="true" fileName="c:/logs/${processname}/${logger}_${shortdate}.log"
layout=">> ${date} ${message} ${exception:format=tostring}" />
</targets>

<rules>
<logger name="Megatec.EngineeringMatrix.AppLogger" minlevel="Trace" writeTo="AppLogFile" />
<logger name="Megatec.EngineeringMatrix.ChangeLogger" minlevel="Trace" writeTo="ChangeLogFile" />
<logger name="Megatec.EngineeringMatrix.DRCLogger" minlevel="Trace" writeTo="DRCLogFile" />
</rules>
</nlog>

明显我写了一个 BAAD 配置,因为无法创建目录 c:\lo?gs

但是,我还是没收到消息
MessageBox.Show(ex.ToString(), @"加载'Megatec.EngineeringMatrix'失败

甚至 AppLogger.Info() 也会跳过异常...

我没有写任何东西来记录,但应用程序不知道...

在调试中,我可以捕获异常,并看到它由 NLog 处理,

如何从我的代码中捕获它?

最佳答案

这是一个老问题,但最近对此有一些更改。

一些异常在过去被 NLog“吃掉”了。例如在 AsyncWrapper (或在 async 上使用属性 <target> )

这已在 NLog 4.3 中修复:

Consistent handling of exceptions (BEHAVIOUR CHANGE) The logging and throwing of exceptions was previously inconsistent. Not all of it was logged to the internal logger and some times they got lost. For example, the async wrapper (or async attribute) was catching all exceptions without a proper rethrow.

This is bad as it is sometimes unclear why NLog isn’t working (got it myself multiple times). This has been fixed in NLog 4.3!

All exceptions are logged to the internal logger The “throwExceptions” option will be respected in all cases Advise: disable throwExceptions in production environments! (this the default)

参见 http://nlog-project.org/2016/04/16/nlog-4-3-has-been-released.html

关于c# - NLog 拒绝抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12497809/

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