将调试日志消息写入文件的更好方法是什么?
目前,我的应用程序以线程安全的方式将调试消息和异常写入日志文件。这非常简单,我不需要任何框架(即 log4net)来执行此操作。
这就是我目前正在做的事情:
private static void Write(string message)
{
Task.Factory.StartNew(() =>
{
try
{
lock (locker)
{
File.AppendAllText(DebugFilePath, message);
InvokeLogWrittenEvent(message);
}
}
catch
{
}
});
}
这样可以吗?还是您认为我应该有一个专用线程,其中有一个 BlockingCollection 来处理添加到集合中的项目?两者都会给我相同的输出,但我不确定是否有任何性能影响。
Currently my application writes debug messages and exceptions to a log file in a thread-safe manner. It's very simple and I do not need any framework (i.e. log4net) to do this.
This is how I am doing it at present:
private static void Write(string message)
{
Task.Factory.StartNew(() =>
{
try
{
lock (locker)
{
File.AppendAllText(DebugFilePath, message);
InvokeLogWrittenEvent(message);
}
}
catch
{
}
});
}
Is this ok or do you think I should have a dedicated thread which has a BlockingCollection that processes items added into the collection? Both will give me the same output, but I'm not sure if there are any performance implications.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在应用程序进程中执行日志记录的问题是,它会影响性能,并在崩溃的情况下给你带来糟糕的结果(如果写入文件,它还会阻止应用程序关闭)。
运行一个单独的主线程来传递日志消息是迄今为止更有效的方式,尽管需要更多的编码,因为它对应用程序性能的影响要小得多。
The problem with performing logging within your application process is that it will effect performance and give you duff results in the case of it crashing (it will also hold up application closure if its writing to a file).
Running a separate primary thread that you pass logging message to is by far a more efficient manner, although requires more coding as it will have far less impact on your applications performance.