请问EventLog日志写入,可以定义类别、事件、用户、计算机吗?
如果有的话?具体属性是?
如果有的话?具体属性是?
解决方案 »
- 【散300分】关于2005自定义控件的问题!
- 数据如何同时放进主表和明细表?
- 有关根据多个DropDownList所选择的项进行SQL查询的问题
- 怎么在asp.net程序中定义一个全局变量?
- 求助:为何不能插入数据?
- 我按如下步骤操作可看不到予览的网页我错在哪???
- 求FarPoint Spread for Web Forms VS2005安装文件
- 怎么把<tr bgcolor=#111111>里面的参数选出来
- 用Asp.Net与Word文件打交道,请问怎么办?
- 欢迎大家来探讨CodeBhind的问题:
- 怎么让My97DatePicker日期控件当天以后的日期不可选择
- 项目架设带iis上只能访问静态页访问数据库异常
if(!EventLog.SourceExists("MySource")){
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatingEventSource");
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";
// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");
把日志写入注册表或文本的方法
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Data;namespace EventsLog
{
public class LogWriter
{
/// <summary>
/// Write EventLog into ~/Log/Error.Log.
/// </summary>
/// <param name="message">Message</param>
public static void WriteToFile(string message)
{
WriteToFile(message, "Error.Log");
} /// <summary>
/// Write EventLog to file of given path.
/// </summary>
/// <param name="message">Message</param>
/// <param name="path">Path of file</param>
public static void WriteToFile(string message, string path)
{
try
{
StreamWriter sw = new StreamWriter(path, true, Encoding.Unicode);
sw.WriteLine("============================================================================");
sw.WriteLine("Time Generated:");
sw.WriteLine(" " + System.DateTime.Now.ToString());
sw.WriteLine("\r\nMessage:");
sw.WriteLine(message);
sw.WriteLine("\r\n\r\n\r\n");
//Must close the stream.
sw.Close();
}
catch { }
} /// <summary>
/// Write EventLog of Error type to register.
/// </summary>
/// <param name="message">Message</param>
public static void WriteErrorToReg(string message)
{
WriteToReg(message, EventLogEntryType.Error);
} /// <summary>
/// Write EventLog of Information type to register.
/// </summary>
/// <param name="message">Message</param>
public static void WriteInfoToReg(string message)
{
WriteToReg(message, EventLogEntryType.Information);
} /// <summary>
/// Write EventLog of Warning type to register.
/// </summary>
/// <param name="message">Message</param>
public static void WriteWarningToReg(string message)
{
WriteToReg(message, EventLogEntryType.Warning);
} /// <summary>
/// Write EventLog to register.
/// </summary>
/// <param name="message">Message</param>
/// <param name="type">Type
/// {
/// Error = EventLogEntryType.Error,
/// Information = EventLogEntryType.Information,
/// Warning = EventLogEntryType.Warning.
/// }</param>
public static void WriteToReg(string message, EventLogEntryType type)
{
try
{
//Get EventLog.
EventLog eventLog = getEventLog(); //Write message to EventLog.
eventLog.WriteEntry(message, type);
}
catch (Exception e)
{
WriteToFile(e.ToString());
}
} /// <summary>
/// Read EventLog from register.
/// </summary>
/// <returns>Return DataTable with all EventLog info.</returns>
public static DataTable ReadFromReg()
{
//Get EventLog.
EventLog eventLog = getEventLog(); //Construct a DataTable Object,with 3 columns : EntryType、TimeGenerated、Message
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("EntryType", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("TimeGenerated", System.Type.GetType("System.DateTime")));
dt.Columns.Add(new DataColumn("Message", System.Type.GetType("System.String"))); //Read EventLog,and add all to DataTable
foreach (EventLogEntry entry in eventLog.Entries)
{
dt.Rows.Add(new object[] { entry.EntryType, entry.TimeGenerated, entry.Message });
} return dt;
} /// <summary>
/// Clear EventLog from register.
/// </summary>
/// <returns>Return is whether successful.</returns>
public static bool ClearFromReg()
{
try
{
//Get EventLog.
EventLog eventLog = getEventLog(); //Clear all EventLog.
eventLog.Clear(); return true;
}
catch
{
return false;
}
} /// <summary>
/// Get EventLog which source is "WebApplication".
/// </summary>
/// <returns>EventLog with special source.</returns>
private static EventLog getEventLog()
{
string source = "WebApplication"; //Make sure EventLog of special source exist.
if (!(EventLog.SourceExists(source)))
{
EventLog.CreateEventSource(source, "Application");
} //New an EventLog Object, and set the source.
EventLog eventLog = new EventLog("Application");
eventLog.Source = source; return eventLog;
}
}
}
我的提问是:请问EventLog日志写入,可以定义类别、事件、用户、计算机吗?
public bool CreateEvtLogFile()
{
//确认事件源是否在本机器上注册
if (!EventLog.Exists(LogName))
{
try
{
EventLog.CreateEventSource(SourceName, LogName, ".");
}
catch (Exception ex)
{
return false;
}
return true;
}
else
return false;
}
/// <summary>
/// 写日志
/// </summary>
/// <param name="level"></param>
public void WriteEvtLog(EventLevel level)
{
if (!EventLog.Exists(LogName))
CreateEvtLogFile();
EventLogEntryType Eventtype = new EventLogEntryType();
switch (level)
{
case EventLevel.EVENT_ERROR:
Eventtype = EventLogEntryType.Error;
break;
case EventLevel.EVENT_FAILUREAUDIT:
Eventtype = EventLogEntryType.FailureAudit;
break;
case EventLevel.EVENT_INFORMATION:
Eventtype = EventLogEntryType.Information;
break;
case EventLevel.EVENT_SUCCESSAUDIT:
Eventtype = EventLogEntryType.SuccessAudit;
break;
case EventLevel.EVENT_WARNING:
Eventtype = EventLogEntryType.Warning;
break;
}
try
{
EventLog.WriteEntry(SourceName, Message, Eventtype, EventID, CateGory, RawData);
}
catch(Exception ex)
{}
}日志写入系统后,发现类别、事件、用户、计算机都是默认的!我想修改这些设置,应该怎么写日志?
string source,
string message,
EventLogEntryType type,
int eventID,
short category,
byte[] rawData
)
string source,
string message,
EventLogEntryType type,
int eventID,
short category,
byte[] rawData )
我用的是这个方法,但是这个方法里面的参数没有日志的类别、用户、计算机、事件