Пример #1
0
        /// <summary>
        /// This constructor accepts a string with the path and filename of a log file to use.
        /// If there is no file one will be created.
        /// If the file cannot be created the log file will be created in the local application data folder for the user.
        /// Be sure to properly escape directory separators (i.e. <c>new EventLogHelper("c:\\temp\\test.log")</c> or <c>new EventLogHelper(@"c:\temp\temp.log")</c>
        /// </summary>
        /// <param name="LogFilePath">The absolute path and filename of the log file.</param>
        public EventLogHelper(string LogFilePath)
        {
            try
            {
                PathName _path = new PathName(LogFilePath);

                if (_path.IsValidFile() || !_path.InvalidAsFile())
                {
                    _logFile = new FileInfo(LogFilePath);
                }
            }
            catch (Exception ex)
            {
                //set this instance to null and write something to the Event Log
                EventLogHelper.WriteApplicationEvent("Could not create log file using " + LogFilePath, EventLogEntryType.Error, "Application");
                //pass exception back up
                throw ex;
            }
        }
Пример #2
0
        /// <summary>
        /// This method writes an error message to the log file specified by the _logfile FileInfo object
        /// </summary>
        public void WriteEventToFile(string Message, EventLogEntryType EntryType)
        {
            //todo: perhaps add more structure layout to this?
            if (_logFile != null)
            {
                //using (StreamWriter sw = _logFile.AppendText())
                using (System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(_logFile.Open(FileMode.Append, FileAccess.Write), System.Text.Encoding.ASCII))
                {
                    try
                    {
                        //sw.WriteLine(EntryType.ToString() + ": " + Message);
                        //xw.WriteLine("{0} {1}: {2}", DateTime.Now, EntryType.ToString(), Message);

                        //xw.WriteElementString("Date", DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
                        //xw.WriteElementString("EntryType", EntryType.ToString());
                        //xw.WriteElementString("Message", Message);

                        xw.WriteStartElement("Event");

                        xw.WriteAttributeString("Date", DateTime.Now.ToShortDateString());
                        xw.WriteAttributeString("Time", DateTime.Now.ToShortTimeString());
                        xw.WriteAttributeString("EntryType", EntryType.ToString());
                        xw.WriteString(Message);

                        xw.WriteEndElement();
                        xw.WriteString("\n");

                        xw.Close();
                    }
                    catch (Exception ex)
                    {
                        EventLogHelper.WriteApplicationEvent("Could not write to log file.\nMessage:  " + Message + "\nException:" + ex.Message, EventLogEntryType.Error, "Application");
                    }
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Writes to this instance's custom log file.
 /// If the user doesn't have admin rights and the write attempt fails, this instance will use the 'Application' Log and Source instead until the source is changed. Pertinent information will be at the bottom of a message indicating it can't find the message source for eventID (0).
 /// Once this exception has occurred, future writes to the event log will ALWAYS go to to the 'failsafe' Application Log with Source=Application
 /// For this reason, make sure the Log and Source objects already exist before using this method without admin rights.
 /// </summary>
 /// <param name="Message">The message to record in the Event Log.</param>
 /// <param name="EntryType">The type of Entry to create (Information, Warning, or Error).</param>
 public void WriteCustomEvent(string Message, EventLogEntryType EntryType)
 {
     try
     {
         _customLog.WriteEntry(Message, EntryType);
     }
     catch (SecurityException)
     {
         //user is non-admin and the specified log or source cannot be created.
         WriteEventFailure(Message, EntryType, ref _customLog);
     }
     catch (ArgumentException)
     {
         //mismatched log - source combination and user is non-admin
         //set log to blank and let it figure out what log to put it in
         _customLog.Log = "";
         this.WriteCustomEvent(Message, EntryType);
     }
     catch (Exception)
     {
         //catch any other exceptions and silently fail, writing to the application log
         EventLogHelper.WriteApplicationEvent("Could not create Event Log entry.\nMessage: " + Message, EventLogEntryType.Error, "Application");
     }
 }