我試圖寫入我的c#代碼的事件查看器,但我得到了奇妙的「對象引用未設置爲對象的實例」消息。我很感謝這段代碼的一些幫助,或者它有什麼問題,或者更好的方法來做到這一點。下面是我對寫入事件日誌: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;
}
}
什麼行是錯誤? – NikolaiDante 2009-07-15 19:13:39
請提供堆棧跟蹤 – 2009-07-15 19:16:31