简体   繁体   English

将调试日志消息写入文件的更好方法是哪种?

[英]Which is the better way for writing debug log messages to a file?

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 (ie log4net) to do this. 这非常简单,我不需要任何框架(即log4net)即可执行此操作。

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? 可以吗?还是您认为我应该有一个专用线程,该线程具有一个BlockingCollection来处理添加到该集合中的项目? Both will give me the same output, but I'm not sure if there are any performance implications. 两者都会给我相同的输出,但是我不确定是否有任何性能影响。

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. 运行一个单独的主线程(将日志消息传递到该主线程)是一种更有效的方式,尽管需要更多的编码,因为这对应用程序性能的影响要小得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM