Exemplo n.º 1
0
		public static void OpenFile(string name, ref MessageLogFile logFile)
		{
			if(logFile != null && logFile._attemptReopen == false && name == logFile.CurrentFile)
				return;

			if(logFile != null) ((IDisposable)logFile).Dispose();
			logFile = null;

			logFile = new MessageLogFile(name);
		}
 public static void LogFileChanged()
 {
     lock (LogFileSync)
     {
         if (_logFile != null && _logFile.CurrentFile != Configuration.CurrentLogFile)
         {
             _logFile.Dispose();
             _logFile = null;
         }
     }
 }
Exemplo n.º 3
0
        public static void OpenFile(string name, ref MessageLogFile logFile)
        {
            if (logFile != null && logFile._attemptReopen == false && name == logFile.CurrentFile)
            {
                return;
            }

            if (logFile != null)
            {
                ((IDisposable)logFile).Dispose();
            }
            logFile = null;

            logFile = new MessageLogFile(name);
        }
        static void PumpMessages(List <EventData> dataList)
        {
            if (dataList == null || dataList.Count == 0)
            {
                return;
            }

            // now that we have some dataList, dispatch to known outputs
            foreach (EventData data in dataList)
            {
                object[] args = data.ToObjectArray();
                #region Trace Output
                //Trace if not debugging (already written on Push if we are debugging)
                if (Configuration.IsDebugging == false && (data.Output & LogOutputs.TraceWrite) == LogOutputs.TraceWrite &&
                    (Configuration.LEVEL_TRACE & data.Level) == data.Level)
                {
                    System.Diagnostics.Trace.WriteLine(String.Format(Configuration.FormatProvider, Configuration.FORMAT_TRACE, args), data.MethodType);
                }
                #endregion
                #region Console Output
                if ((data.Output & LogOutputs.Console) == LogOutputs.Console &&
                    (Configuration.LEVEL_CONSOLE & data.Level) == data.Level)
                {
                    ConsoleColor orig = Console.ForegroundColor;
                    try
                    {
                        TextWriter   output = Console.Out;
                        ConsoleColor color  = Console.ForegroundColor;
                        if (data.Level == LogLevels.Error || data.Level == LogLevels.Critical)
                        {
                            color  = ConsoleColor.Red;
                            output = Console.Error;
                        }
                        else if (data.Level == LogLevels.Warning)
                        {
                            color = ConsoleColor.Yellow;
                        }
                        else if (data.Level == LogLevels.Info)
                        {
                            color = ConsoleColor.White;
                        }
                        else
                        {
                            color = ConsoleColor.Gray;
                        }

                        if ((Configuration.LogOption & LogOptions.ConsoleColors) == LogOptions.ConsoleColors)
                        {
                            Console.ForegroundColor = color;
                        }
                        output.WriteLine(String.Format(Configuration.FormatProvider, Configuration.FORMAT_CONSOLE, args));
                    }
                    catch (Exception e) { LogUtils.LogError(e); }
                    finally
                    {
                        if ((Configuration.LogOption & LogOptions.ConsoleColors) == LogOptions.ConsoleColors)
                        {
                            Console.ForegroundColor = orig;
                        }
                    }
                }
                #endregion
                #region Log File Output
                if ((data.Output & LogOutputs.LogFile) == LogOutputs.LogFile &&
                    (Configuration.LEVEL_LOGFILE & data.Level) == data.Level)
                {
                    lock (LogFileSync)
                    {
                        // check to see if we can still use the current log file
                        if (_logFile != null && _logFile.IsOpen)
                        {
                            try
                            {
                                //has the size exceeded max?
                                if (_logFile.TextWriter.BaseStream.Position > Configuration.FILE_SIZE_THREASHOLD)
                                {
                                    _logFile.Dispose();                                    //need a new file...
                                }
                                //has the configuration changed?
                                if (_logFile.CurrentFile != Configuration.CurrentLogFile)
                                {
                                    _logFile.Dispose();
                                }
                            }
                            catch (ObjectDisposedException)
                            { _logFile.Dispose(); }
                        }

                        //open a new log file if needed
                        if (_logFile == null || false == _logFile.IsOpen)
                        {
                            MessageLogFile.OpenFile(Configuration.CurrentLogFile, ref _logFile);
                        }

                        //if all is well, log to file...
                        if (_logFile != null && _logFile.IsOpen)
                        {
                            try
                            {
                                if (_logFile.IsXmlFile)
                                {
                                    _logFile.TextWriter.WriteLine(data.ToXml());
                                    //keep the end of root written in the file so the xml is always valid
                                    long pos = _logFile.TextWriter.BaseStream.Position;
                                    _logFile.TextWriter.WriteLine("</log>");
                                    _logFile.TextWriter.BaseStream.Position = pos;
                                }
                                else
                                {
                                    _logFile.TextWriter.WriteLine(String.Format(Configuration.FormatProvider, Configuration.FORMAT_LOGFILE, args));
                                }
                            }
                            catch (ObjectDisposedException e)
                            { _logFile.Dispose(); LogUtils.LogError(e); }
                            catch (System.IO.IOException e)
                            { _logFile.Dispose(); LogUtils.LogError(e); }
                            catch (Exception e)
                            { LogUtils.LogError(e); }
                        }
                    }
                }
                #endregion
                #region Event Log Writer
                if ((data.Output & LogOutputs.EventLog) == LogOutputs.EventLog &&
                    (Configuration.LEVEL_EVENTLOG & data.Level) == data.Level)
                {
                    //we will only pass a selective set to the event log... write through, warning and higher
                    if (EventLogSource.IsWorking)
                    {
                        int eventId;
                        try
                        {
                            EventLog log = EventLogSource.GetLogFor(data, out eventId);
                            if (log != null)
                            {
                                log.WriteEntry(
                                    String.Format(Configuration.FormatProvider, Configuration.FORMAT_EVENTLOG, args),
                                    data.Level == LogLevels.Error || data.Level == LogLevels.Critical ? EventLogEntryType.Error :
                                    data.Level == LogLevels.Warning ? EventLogEntryType.Warning : EventLogEntryType.Information,
                                    eventId);
                            }

                            EventLogSource.IsWorking = true;
                        }
                        catch (Exception e)
                        {
                            if (EventLogSource.IsWorking)
                            {
                                LogUtils.LogError(e);
                            }
                            EventLogSource.IsWorking = false;
                        }
                    }
                }
                #endregion
            }

            LogEventArgs    evntArgs = new LogEventArgs(dataList);
            LogEventHandler handler  = InternalLogWrite;
            if (handler != null)
            {
                foreach (LogEventHandler h in handler.GetInvocationList())
                {
                    try
                    {
                        h(null, evntArgs);
                    }
                    catch (ThreadAbortException)
                    { return; }
                    catch (Exception e)
                    { LogUtils.LogError(e); }
                }
            }
        }
Exemplo n.º 5
0
		public static void LogFileChanged()
		{
			lock (LogFileSync)
			{
				if (_logFile != null && _logFile.CurrentFile != Configuration.CurrentLogFile)
				{
					_logFile.Dispose();
					_logFile = null;
				}
			}
		}