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를 완전히 오해 했습니까?
안녕하세요 매트, 두 번째 방법을 사용하려고하는 동안 다음 오류가 발생합니다. "컴퓨터의 이벤트 로그 'SSDAppLog'. ' 존재하지 않는다." 어떤 제안? –
@Tom Wright, 위의 업데이트를 참조하십시오. –
감사합니다 매트 - 매력처럼 작동합니다. –