代码之家  ›  专栏  ›  技术社区  ›  PushCode

C写入事件查看器

  •  54
  • PushCode  · 技术社区  · 15 年前

    我正试图用我的C代码写入事件查看器,但我得到了一条美妙的“对象引用未设置为对象实例”消息。我希望能得到一些关于这段代码的帮助,要么是它有什么问题,要么是更好的方法。以下是我要写入事件日志的内容:

    private void WriteToEventLog(string message)
    {
        string cs = "QualityDocHandler";
        EventLog elog = new EventLog();
        if (!EventLog.SourceExists(cs))
        {
            EventLog.CreateEventSource(cs, cs);
        }
        elog.Source = cs;
        elog.EnableRaisingEvents = true;
        elog.WriteEntry(message);
    }
    

    这就是我要说的:

    private readonly Random _rng = new Random();
    private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private string RandomString(int size)
    {
        try
        {
            char[] buffer = new char[size];
            for (int i = 0; i < size; i++)
            {
                buffer[i] = _chars[_rng.Next(_chars.Length)];
            }
            return new string(buffer);
        }
        catch (Exception e)
        {
            WriteToEventLog(e.ToString());
            return null;
        }
    }
    
    3 回复  |  直到 8 年前
        1
  •  87
  •   Brian Rudolph    15 年前

    问题可能是您试图在不存在的日志中创建事件源。您需要指定“应用程序”日志。

    尝试将其更改为:

    if (!EventLog.SourceExists(cs))
       EventLog.CreateEventSource(cs, "Application");    
    
    EventLog.WriteEntry(cs, message, EventLogEntryType.Error);
    

    另外:在SharePoint内部,如果应用程序以登录用户身份运行(通过Windows身份验证或委派),则用户将无权创建事件源。如果是这种情况,一个技巧是使用线程池线程创建事件,创建线程时,该线程将具有运行应用程序池的用户的安全上下文。

        2
  •  22
  •   Nelson    15 年前

    下面是我如何实现事件日志记录。我创建了一个通用的ilogger接口,以便在不同的日志机制中进行交换:

    interface ILogger
    {
        void Debug(string text);
    
        void Warn(string text);
    
        void Error(string text);
        void Error(string text, Exception ex);
    }
    

    我的实现类非常简单:

    class EventLogger : ILogger
    {
        public void Debug(string text)
        {
            EventLog.WriteEntry("MyAppName", text, EventLogEntryType.Information);
        }
    
        public void Warn(string text)
        {
            EventLog.WriteEntry("MyAppName", text, EventLogEntryType.Warning);
        }
    
        public void Error(string text)
        {
            EventLog.WriteEntry("MyAppName", text, EventLogEntryType.Error);
        }
    
        public void Error(string text, Exception ex)
        {
            Error(text);
            Error(ex.StackTrace);
        }
    }
    

    注意,我没有实例化事件日志。要使用logger类,我只有以下引用(您可以通过静态工厂方法返回此引用):

    private static readonly ILogger log = new EventLogger();
    

    实际用途如下:

    try
    {
        // business logic
    }
    catch (Exception ex)
    {
        log.Error("Exception in MyMethodName()", ex);
    }
    
        3
  •  1
  •   Dutt93    8 年前
       private void WriteEventLogToFile()
        {
            try
            {
                using (EventLog eventLog = new EventLog("Application"))
                {
                 // source for your event 
                    eventLog.Source = "IAStorDataMgrSvc";
    
                 // Syntax details
                // eventLog.WriteEntry("details",type of event,event id);
                 eventLog.WriteEntry("Hard disk Failure details", EventLogEntryType.Information, 11);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }