ASP.NET程序向Event Viewer写入Event Log

来源:互联网 发布:神武2手游多开软件 编辑:程序博客网 时间:2024/05/01 22:51

/*By Jiangong SUN*/

在web.config中的appsettings中写入


<!--Event Log--><add key="EventLog" value="Application" /><add key="EventSource" value="Project" />

在项目中global.asax中

protected void Application_Start(object sender, EventArgs e){   Log.Configure(Settings.EventSource, Settings.EventLog); }

在Log类中

public class Log    {        static string source;        public static void Configure(string eventSource, string eventLog)        {            // Create the source, if it does not already exist.            if (!EventLog.SourceExists(eventSource,"."))            {                EventLog.CreateEventSource(eventSource, eventLog);            }            source = eventSource;        }        public static void Error(string message)        {            Write(message, EventLogEntryType.Error);        }        public static void Error(string message, Exception e)        {            Write(message + "\n\n" + e.ToString(), EventLogEntryType.Error);        }        public static void Info(string message)        {            Write(message, EventLogEntryType.Information);        }        public static void Fatal(string message, Exception e)        {            Write(message + "\n\n" + e.ToString(), EventLogEntryType.Error);        }        static void Write(string message, EventLogEntryType type)        {            EventLog myLog = new EventLog();            myLog.Source = source;            myLog.WriteEntry(message, type);        }    }

在Settings类中


public static class Settings

    {        public static string EventSource        {            get            {                return WebConfigurationManager.AppSettings["EventSource"];            }        }        public static string EventLog        {            get            {                return WebConfigurationManager.AppSettings["EventLog"];            }        }
}

然后打开注册表

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application

创建一个新的key,key的名字就是项目的名字Project



原创粉丝点击