C#写系统日志
/// <summary>
/// 写系统日志
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
string message="刘习金的测试主文本,时间"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string sourcename = "dnsinfo";
EventLogEntryType errotype=EventLogEntryType.SuccessAudit;
try
{
EventLog mylog = new EventLog();
if (!EventLog.SourceExists(sourcename))
{
EventLog.CreateEventSource(sourcename, sourcename);
}
mylog.Source = sourcename;
mylog.MaximumKilobytes = 1024 * 20;
mylog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 30);
mylog.WriteEntry(message, errotype);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}