开心

生成日志

1.数据库日志

把错误写入数据库的日志中。

2.系统日志

全局应用程序类(.asax)实现: 导入System.Diagnostics ;

void Application_Error(object sender,EventArgs e)

{  Exception errinfo =Server.GetLastError();  string Message="url:"+Request.Path+errinfo.ToString();  string SourceName="mylog";  

if(!EventLog.SourceExists(SourceName))  

{   EventLog.CreateEventSource(SourceName,"logweb");  }

EventLog log=new EventLog(); log.Source=LogName;

log.WriteEntry(Message,EventLogEntryType.Error);

//删除系统日志

//EventLog.DeleteEventSource(SourceName);

//EventLog.Delete("logweb");

}

1:事件日志名(logName):“事件查看器”中的每一项,如“应用程序”、“Internet Explorer”、“安全性”和“系统”都是日志(严格地说是日志的显示名字)

2:事件源:列表中的“来源”,创建时和事件日志相关联;

3:事件类型:包括“信息”、“错误”等;

 

下面介绍事件日志的基本操作:

1:创建日志:我没找到直接创建日志的方法,日志应该都是通过下面的创建事件源来间接创建;

2:创建事件源:静态方法EventLog.CreateEventSource(string sourceName, string LogName); //参数分别表示事件源名和日志名

   功能说明:在某个事件日志中创建事件源,如果事件日志不存在,则自动创建;

3:删除日志:静态方法EventLog.Delete(string logName);

4:删除事件源:静态方法EventLog.DeleteEventSource(string sourceName);

5:判断日志是否存在:静态方法EventLog.Exists(string logName);

6:判断事件源是否存在:静态方法EventLog. SourceExists (string sourceName);

7:写日志:使用EventLog类的实例调用方法WriteEntry(string logDesc, EventLogEntryType.Information); //或者EventLogEntryType.Error

 

在注册表中也生成了相应的文件夹:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\eventlog

日志文件默认存放路径:C:\WINDOWS\system32\config

 读、写日志:

 1 public void WriteLog(string logName, string SourceName, string LogText, EventLogEntryType type)
 2 
 3         {
 4 
 5             // Create an EventLog instance and assign its source.
 6 
 7             EventLog el = new EventLog();
 8 
 9             try
10 
11             {
12 
13                 // Create the source, if it does not already exist.
14 
15                 if (!EventLog.SourceExists(SourceName))
16 
17                 {
18 
19                     if (EventLog.Exists(logName))
20 
21                     {
22 
23                         el.Log = logName;
24 
25                     }
26 
27                     else
28 
29                     {
30 
31                         EventLog.CreateEventSource(SourceName, logName);
32 
33                     }                   
34 
35                 }             
36 
37                 el.Source = SourceName;
38 
39                 el.WriteEntry(LogText, type);
40 
41             }
42 
43             catch (Exception ex)
44 
45             {
46 
47                 el.WriteEntry(ex.Message, EventLogEntryType.Error);
48 
49             }
50 
51  
52 
53         }
54 
55  
56 
57 调用上述方法:this.WriteLog("测试日志", " testSource", " my log ", EventLogEntryType.Information);
WriteLog
 1 string[] logTypes = new string[] { "Application", "Security", "System" };
 2 
 3  
 4 
 5    foreach (string t in logTypes)
 6 
 7    {
 8 
 9        EventLog e = new EventLog();
10 
11        e.Log = t;
12 
13        foreach (EventLogEntry l in e.Entries)
14 
15        {
16 
17            if (l.EntryType == EventLogEntryType.Error)
18 
19            {
20 
21                Console.WriteLine(l.Message);
22 
23            }
24 
25        }
26 
27    }
readLog

 

3.文本日志

 

Trace Debug类提供了对日志记录的支持,他们允许单一的写日志方法(Write(),WriteLine())把日志写到多个目标上,列如界面、页面上或者文件,也可以写入事件日志中。

跟踪消息由侦听器(Listener)来收。侦听器的用途是收集、存储和路由跟踪消息。侦听器会将跟踪输出定向到适当的目标,如:日志、窗口或文本文件。

1.DefaultTraceListener 使调试消息出现在输出窗口中。

2.TextWriterTraceListner 将输出写入控制台或文件。

3.EventLogTraceListener 将输出重定向到事件日志。

4.ConsoleTraceListener 向控制台中写入跟踪和调试消息。

5.XmlWriterTraceListener 将跟踪或调试输出到xml文档。

6.DelimitedListTraceListener 跟踪输出采用分隔符分隔文本格式。

using System.Diagnostics;

void textLog() {

Trace.Listeners.Add(new TextWriterTraceListner(@"d:\log.txt"));

Trace.Listeners.Add(new EventLogTraceListener("logweb"));//可以Add多个listener.

Trace.AutoFlush=true; Trace.WriteLine("my log message"); }

4.log4net 日志框架

是一个第三方开源组件,它设计的主要目的是组合,生成日志信息,同时将配置保存到各种存储介质或者展现平台中,在实际项目中,Log4net可以保存系统运行情况,可以在系统出现异常时,根据保存的日志信息,查看当时系统的状态

using log4net; private static ILog Log=log4net.LogManager.GetLogger(typeof(类名));

Log.Debug("start");

Log.Error("run error",new ApplicationException("test"));

Log.Debug("end');

参考:

http://www.tuicool.com/articles/FrYfQvb

注;config文件需要配置。

posted @ 2016-02-20 21:50  大喜  阅读(183)  评论(0)    收藏  举报
坦然 会增进信任 - 信任 感情才会升华