控制台应用程序未在 AppDomain.CurrentDomain.UnhandledException 中记录 NLOG 错误
下面的代码没有记录到我的数据库。是否是因为控制台应用程序关闭得太快,如果是,我该如何防止这种情况?
private static ILogger _logger;
static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
{
_logger.Error((Exception)e.ExceptionObject);
}
private static void Main(string[] args)
{
var container = new Container();
_logger = container.GetInstance<ILogger>();
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
throw new Exception("test");
The below code isn't logging to my database. Is it because the console application closes too soon and if so how can I prevent this?
private static ILogger _logger;
static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
{
_logger.Error((Exception)e.ExceptionObject);
}
private static void Main(string[] args)
{
var container = new Container();
_logger = container.GetInstance<ILogger>();
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
throw new Exception("test");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在离开
UnhandledExceptionTrapper
方法之前调用LogManager.Shutdown()
。这会在内部调用LogManager.Flush()
请参阅 NSLog Flush 文档。
其他故障排除
在调试控制台中,如果连接上发生 SQL 错误等情况,则会显示错误消息,但默认情况下您不会看到配置是否无效。
由于 NLog 提供了广泛的配置选项,因此很容易出现一些小的配置错误,从而导致不记录日志。一个典型的例子是不正确的
ConnectionString
或DBProvider
。例如,假设您配置了不正确的 DBProvider。
例如,你可以打电话
,但它会默默地失败。
但是,如果您
在测试日志调用之前插入,您将在调试控制台中看到相应的消息。例如,如果您有一个无效的 DBProvider,例如
Npgsql.NpgsqlConnection1, Npgsql
(请注意其中的无效1
),您会看到哪个可以清楚地指示发生了什么错误的。
更多诊断
要查看更多诊断信息,您可以添加以下行:
除其他外,您可以查看连接何时打开、写入了哪些数据以及有关刷新的信息(小样本摘录):
< strong>自包含示例
它在数据库中提供了以下条目:
You can call
LogManager.Shutdown()
before leaving yourUnhandledExceptionTrapper
method. This calls internallyLogManager.Flush()
whichsee NSLog Flush documentation.
Additional Troubleshooting
In the debug console, error messages are displayed if, for example, SQL errors occur on the connection, but by default you do not see if you have an invalid configuration.
Since NLog offers extensive configuration options, it is easy to make a small configuration errors that results in no logging. A typical example would be an incorrect
ConnectionString
orDBProvider
.As an example, suppose you have configured an incorrect DBProvider.
You can e.g call
and it would just silently fail.
However, if you insert
before the test log call, you will see appropriate messages in the debug console. For example, if you have an invalid DBProvider like
Npgsql.NpgsqlConnection1, Npgsql
(note the invalid1
in it), you would seewhich would give a clear indication of what went wrong.
More Diagnostics
To see even more diagnostics, you can add the following lines:
Among other things, you can see when the connection is opened, what data is written and information about flushing, a small sample excerpt:
Self-Contained Example
Which gives the followoing entries in the DB: