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

30 篇文章 0 订阅

/*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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值