[英]ThrowIfCancellationRequested doesn't seem to throw any exceptions
[英]NLog doesn't throw exceptions
每当数据库目标发生连接故障时,我都希望NLog引发异常并传递给我的应用程序。
这是我的NLog配置文件:
<nlog autoReload="true"
throwExceptions="true"
internalLogLevel="Debug" internalLogFile="c:\temp\nlog-internal.log">
现在,当配置文件中有错误时,它会在配置时抛出运行时异常。 它还在内部日志文件中记录有关连接失败的异常,但不会将异常返回给调用应用程序。
我还尝试将Logger.Log方法包装在try-catch
,以确保它不会吞噬它,但是Logger.Log语句已成功执行。
还有什么可以在应用程序中捕获异常的。 我只想准备未记录的日志事件的缓存,然后在连接再次可用时再登录回SQL Server。
编辑:
LogManager.ThrowException
在运行时也为true。 DatabaseTarget
的Write
方法包装在try / catch中,它将仅重新抛出特定类型的异常( StackOverflow
, ThreadAbort
, OutOfMemory
和NLogConfiguration
)。 您注意到,其他例外将仅写入内部日志。
如果要实现一个自定义目标 ,您可以在其中子类化并覆盖此方法,这是相当琐碎的。
protected override void Write(AsyncLogEventInfo[] logEvents)
{
var buckets = SortHelpers.BucketSort(logEvents, c => this.BuildConnectionString(c.LogEvent));
try
{
foreach (var kvp in buckets)
{
foreach (AsyncLogEventInfo ev in kvp.Value)
{
try
{
this.WriteEventToDatabase(ev.LogEvent);
ev.Continuation(null);
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
// in case of exception, close the connection and report it
InternalLogger.Error("Error when writing to database {0}", exception);
this.CloseConnection();
ev.Continuation(exception);
}
}
}
}
finally
{
if (!this.KeepConnection)
{
this.CloseConnection();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.