2010-06-23 2 views
1

EventLog를 사용하여 C# 응용 프로그램에서 로깅 클래스를 지원합니다. -뿐만 아니라 그 지정된 소스에서EventLog 입력을 한 소스로 제한하는 방법은 무엇입니까?

class Logger 
{ 
    private EventLog eventLog; 
    private ListView listViewControl = null; 
    private String logSource = "SSD1"; 

    public Logger(ListView _listViewControl = null, string _logFileName = null) 
    { 
     if (!EventLog.SourceExists("SSD1")) 
      EventLog.CreateEventSource(logSource, "Application"); 
     eventLog = new EventLog(); 
     eventLog.Source = logSource; 
     addListView(_listViewControl); 
     logFilename = _logFileName; 
    } 

    public void addListView(ListView newListView) 
    { 
     if (eventLog.Entries.Count > 0) 
     { 
      foreach (EventLogEntry entry in eventLog.Entries) 
      { 
       listViewControl.Items.Add(buildListItem(entry)); 
      } 
     } 
    } 

    public void LogInformation(string message) 
    { 
     LogEntry(message, EventLogEntryType.Information); 
    } 

    private void LogEntry(string message, EventLogEntryType logType) 
    { 
     eventLog.WriteEntry(message, logType); 
     if (listViewControl != null) 
     { 
      updateListView(); 
     } 
    } 

    private void updateListView() 
    { 
     listViewControl.Items.Add(buildListItem(eventLog.Entries[eventLog.Entries.Count-1])); 
    } 

    private ListViewItem buildListItem(EventLogEntry entry) 
    { 
     string[] eventArray = new string[3]; 
     eventArray[0] = entry.Message + " (" + entry.Source +")"; 
     eventArray[1] = entry.EntryType.ToString(); 
     eventArray[2] = entry.TimeGenerated.ToString("dd/MM/yyyy - HH:mm:ss"); 
     return new ListViewItem(eventArray); 
    } 

문제는, ListView에 로그의 전체로 채워됩니다 : (Previously...)는 여기에 그 클래스의 아래로 깍 사본입니다. 다음은 출력의 스크린 샷입니다 :

Entries from all sources http://img341.imageshack.us/img341/6185/entriesfromalllogs.png

(이 이미지는, 각 항목의 소스는 메시지 후 괄호 안에 있습니다.)

내가 이벤트 로그가 사람들을 반환받을 수 있나요 어떻게 내 출처의 항목? EventLog를 완전히 오해 했습니까?

답변

2

EventLog.Source 회원은 필터로 작동하지 않습니다. 당신이 EventLog 인스턴스의 Log 구성원에 대한 문자열을 지정하지 않기 때문에 EventLog

To read from a log, specify the Log name and MachineName (server computer name) for the EventLog. It is not necessary to specify the Source, as a source is required only for writing to logs. The Entries member is automatically populated with the event log's list of entries.

에 대한 MSDN 문서에 따르면, 모든 것을지고 있습니다.

내 머리 꼭대기에는이 문제를 처리 할 수있는 몇 가지 방법이 있습니다.

먼저 소스 이름을 필터링하려면 buildListItem()을 수정하십시오. 이것은 비교적 간단합니다.

둘째, 자체 로그를 만드십시오. Application 로그에 기록하는 대신 응용 프로그램 전용 로그를 작성하십시오. 당신은 당신의 생성자를 변경하여이 작업을 수행 할 수 있습니다

public Logger(ListView _listViewControl = null, string _logFileName = null) 
{ 
    if (!EventLog.SourceExists("SSD1")) 
     EventLog.CreateEventSource("SSD1", "TomWrightApplication"); 
    eventLog = new EventLog("TomWrightApplication", ".", "SSD1"); 
    addListView(_listViewControl); 
    logFilename = _logFileName; 
} 

모든 기록은 이제 TomWrightApplication 로그가 아닌 일반 Application 로그로 이동합니다. 이 성공적으로 작동

static void Main() 
{ 
    if (!EventLog.SourceExists("SSD1")) 
     EventLog.CreateEventSource("SSD1", "SSDAppLog"); 
    EventLog log = new EventLog("SSDAppLog", ".", "SSD1"); 
    log.WriteEntry("this is a test"); 
} 

SSD1 소스 이름이 이미 다른 로그에 등록되어 ... 않는 :


톰, 나는 단순히 다음을 수행하는 테스트 프로젝트를 가지고있다. 원본 이름은 모든 이벤트 로그에서 고유해야합니다. 따라서 이미 SSD1을 응용 프로그램 로그에 등록한 경우 새 EventLog를 만들 때 위의 코드가 실패합니다. EventLog.DeleteEventSource()을 사용하여 응용 프로그램 로그에서 SSD1 원본 이름을 제거해보십시오 (시스템에서이 작업을 한 번만 실행하십시오). 위의 코드가 작동해야합니다 (관리자 권한이 있다고 가정).

+0

안녕하세요 매트, 두 번째 방법을 사용하려고하는 동안 다음 오류가 발생합니다. "컴퓨터의 이벤트 로그 'SSDAppLog'. ' 존재하지 않는다." 어떤 제안? –

+0

@Tom Wright, 위의 업데이트를 참조하십시오. –

+0

감사합니다 매트 - 매력처럼 작동합니다. –

1

이벤트 로그에서 읽는 것을 시도한 적이 없으므로 필터를 사용하는 방법을 잘 모르겠지만 이벤트를 Application 로그에 쓰는 대신 자신의 로그를 만들어 새 로그를 만들 수 있습니다. SSD 또는 무언가를 쓰고 글을 쓰거나 읽으면 그곳의 이벤트 일뿐입니다.

관련 문제