如何写入Windows事件以及如何在以后查看?

时间:2011-02-25 07:01:20

标签: c# winforms

如何将C#winforms程序中的错误写入Windows事件?

以后我怎么能看到它?

有没有工具可以看到它?

我可以获得有关如何操作的示例代码吗?

提前致谢

3 个答案:

答案 0 :(得分:2)

阅读本文 - http://support.microsoft.com/kb/307024 本分步指南介绍了如何使用Microsoft .NET Framework将自己的条目添加到操作系统的事件日志中。

答案 1 :(得分:0)

使用log4net之类的日志记录库。使用这样的库,您可以配置几个“appenders”,登录Windows事件日志,文件,发送邮件等。在网上你可以找到很多例子。 (提示:log4net windows事件查看器)

答案 2 :(得分:0)

以下是来自MSDN的示例:

using System;
using System.Diagnostics;
using System.Threading;

class MySample{

    public static void Main(){

        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource"))
        {
             //An event log source should not be created and immediately used.
             //There is a latency time to enable the source, it should be created
             //prior to executing the application that uses the source.
             //Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatedEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";

        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");

    }
}