每当记录事件时,将Windows事件日志存储到数据库中

时间:2017-11-02 09:20:17

标签: c# sql-server-2008-r2 event-log

//我创建了一个应用程序,它从事件查看器获取事件日志并存储在数据库中,现在我想在记录特定事件时触发该代码 // thi Code获取事件日志并将其存储在数据库中...

try
    {
                    EventLog el = new EventLog();
                    el.Log = "Security";
                    el.MachineName = ".";
                    Query q = new Query();
                    foreach (EventLogEntry en in el.Entries )
                    {
                        if (if en.EventID==4688)
                        {
                            q.Insert("insert into EventLog (MachineName,Username,InstanceID,EventMsg,TimeGenerated) values ('" + en.MachineName + "', '" + en.UserName + "','" + en.InstanceId + "','" + en.Message + "','" + en.TimeGenerated + "')", q.Con());
                        }

                    }

                }
                catch (Exception x)
                {
                    MessageBox.Show(x.Message );
                }

0 个答案:

没有答案