请问EventLog日志写入,可以定义类别、事件、用户、计算机吗?
如果有的话?具体属性是?

解决方案 »

  1.   

    // Create the source, if it does not already exist.
            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.");
      

  2.   


    把日志写入注册表或文本的方法
    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;
            }
        }

      

  3.   

    楼1 和 楼3都没有解决问题!
    我的提问是:请问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)
                {}
            }日志写入系统后,发现类别、事件、用户、计算机都是默认的!我想修改这些设置,应该怎么写日志?
      

  4.   

    EventLog.WriteEntry (
    string source,
    string message,
    EventLogEntryType type,
    int eventID,
    short category,
    byte[] rawData
    )
      

  5.   

    EventLog.WriteEntry ( 
    string source, 
    string message, 
    EventLogEntryType type, 
    int eventID, 
    short category, 
    byte[] rawData )
    我用的是这个方法,但是这个方法里面的参数没有日志的类别、用户、计算机、事件