54

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;
    }
}
4

3 に答える 3

87

問題は、存在しないログにイベント ソースを作成しようとしている可能性があります。「アプリケーション」ログを指定する必要があります。

次のように変更してみてください。

if (!EventLog.SourceExists(cs))
   EventLog.CreateEventSource(cs, "Application");    

EventLog.WriteEntry(cs, message, EventLogEntryType.Error);

また、sharepoint 内で、アプリが (Windows 認証または委任を介して) ログイン ユーザーとして実行されている場合、ユーザーはイベント ソースを作成するためのアクセス権を持ちません。この場合、ThreadPool スレッドを使用してイベントを作成するのが 1 つの秘訣です。このスレッドが作成されると、アプリケーション プールを実行しているユーザーのセキュリティ コンテキストが設定されます。

于 2009-07-15T19:29:27.523 に答える
22

イベントログを実装する方法は次のとおりです。汎用の 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);
    }
}

EventLog をインスタンス化していないことに注意してください。私のロガー クラスを使用するには、次の参照を用意するだけです (静的ファクトリ メソッドによってこれを返すことができます)。

private static readonly ILogger log = new EventLogger();

で、実際の使い方はこんな感じ。

try
{
    // business logic
}
catch (Exception ex)
{
    log.Error("Exception in MyMethodName()", ex);
}
于 2009-07-15T19:28:48.647 に答える
1
   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;
        }
    }
于 2017-02-06T09:14:09.503 に答える