即使使用管理员权限,也未创建事件日志

时间:2014-10-23 19:36:12

标签: c# asp.net windows events logging

我使用以下代码:

public static void ErrorRoutine(Exception e, string obj, string method)
{
    //EventLog.Delete("Info3070");  // uncomment this line to delete log
    EventLog log = new EventLog();
    log.Source = "Helpdesk Case1";
    log.Log = "Info3070";

    if (e.InnerException != null)
    {
        log.WriteEntry("Error in Models, object = " + obj + ", method = " + method + ", inner exception = " +
            e.InnerException.Message, EventLogEntryType.Error);
        throw e.InnerException;
    }
    else
    {
        log.WriteEntry("Error in Models, object = " + obj + ", method = " + method + ", message = " + e.Message,
            EventLogEntryType.Error);
        throw e;
    }
}

应该在Windows事件查看器中记录事件日志中的错误,但即使以管理员身份运行也不会这样做。

2 个答案:

答案 0 :(得分:1)

您必须首先定义您的来源,我怀疑"帮助台案例1"不知道。

答案 1 :(得分:0)

如果不存在具有指定参数的事件日志,则应在之前创建:

const string sSource = "Helpdesk Case1";
const string sLog = "Info3070";

if (!EventLog.SourceExists(sSource))
    EventLog.CreateEventSource(sSource, sLog);

EventLog log = new EventLog { Source = sSource, Log = sLog };