Пример #1
0
        /// <summary>
        /// Writes the message to the log file or the event log based on the instance's configuration
        /// </summary>
        /// <param name="Msg">A string suitable for string.Format</param>
        /// <param name="MsgLevel">Message type</param>
        /// <param name="Args">A params list suitable for string.Format (or null)</param>

        public void Message(string Msg, LogLevel MsgLevel, params object[] Args)
        {
            try
            {
                string FormattedMsg = ParseMsg(Msg, Args);

                // Windows event log

                if (LoggingEnabled && LoggingOutput == LogOutput.ToEventLog && LoggingLevel.CompareTo(MsgLevel) >= 0 && LoggingAppName != null)
                {
                    if (!EventLog.SourceExists(LoggingAppName))
                    {
                        // If our application is not registered as a source with the event logging
                        // subsystem then go ahead and register it

                        EventLog.CreateEventSource(LoggingAppName, "Application");
                    }
                    EventLog.WriteEntry(LoggingAppName, FormattedMsg, XlatToWin(MsgLevel));
                }

                // Text file log

                else if (LoggingEnabled && LoggingOutput == LogOutput.ToFile && LoggingLogFile != null && LoggingLevel.CompareTo(MsgLevel) >= 0)
                {
                    using (StreamWriter sw = new StreamWriter(LoggingLogFile, true)) // true means append
                    {
                        sw.WriteLine(string.Format("{0} -- {1,-4} -- {2}", DateTime.Now, XlatToStr(MsgLevel), FormattedMsg));
                    }
                }

                // SQL log

                else if (LoggingEnabled && LoggingOutput == LogOutput.ToDatabase && LoggingLevel.CompareTo(MsgLevel) >= 0)
                {
                    LogToSQL(XlatToStr(MsgLevel), FormattedMsg);
                }

                // Console log

                else if (LoggingEnabled && LoggingOutput == LogOutput.ToConsole && LoggingLevel.CompareTo(MsgLevel) >= 0)
                {
                    Console.WriteLine(FormattedMsg);
                }
            }
            catch (Exception Ex)
            {
                if (LoggingOutput == LogOutput.ToDatabase) // If we get an error trying to log to the database, switch to text file logging
                {
                    if (LoggingLogFile != null)
                    {
                        LoggingOutput = LogOutput.ToFile;
                        try
                        {
                            using (StreamWriter sw = new StreamWriter(LoggingLogFile, true)) // true means append
                            {
                                sw.WriteLine(string.Format("{0} -- {1,-4} -- {2}", DateTime.Now, "ERR", string.Format("An error occurred attempting to log to the database. The error was: {0}. Switching over to text file logging. Stack trace follows: {1}", Ex.Message, Ex.StackTrace)));
                            }
                        }
                        catch { } // no meaningful action
                    }
                    else          // ok no text log - use event log
                    {
                        LoggingOutput = LogOutput.ToEventLog;
                        try
                        {
                            if (!EventLog.SourceExists(LoggingAppName))
                            {
                                // If our application is not registered as a source with the event logging
                                // subsystem then go ahead and register it

                                EventLog.CreateEventSource(LoggingAppName, "Application");
                            }
                            EventLog.WriteEntry(LoggingAppName, string.Format("An error occurred attempting to log to the database. The error was: {0}. Switching over to Windows Event Log logging. Stack trace follows: {1}", Ex.Message, Ex.StackTrace), EventLogEntryType.Error);
                        }
                        catch { } // no meaningful action
                    }
                }
            }
        }
        public bool IsEnabled(LogLevel logLevel)
        {
            int comparison = logLevel.CompareTo(MigratorLogging.configMinimumLogLevel);

            return(comparison >= 0);
        }