예제 #1
0
        /// <summary>
        /// Handle actualy logging messages to console or log file with specified log level and tag. Also tracks the calling methog name/signature and time stamp for refference.
        /// </summary>
        /// <param name="msg">Message to log</param>
        /// <param name="logLevel">Level of importance. Visiability level</param>
        /// <param name="tag">Tag to categorize messages</param>
        /// <param name="stackDepth">Number of call frames from the calling method (default is 2, this & D)</param>
        private static void LogMessage(string msg, LogLevel logLevel = LogLevel.Information, string tag = null, int stackDepth = 2)
        {
            MethodBase mb = new StackTrace().GetFrame(stackDepth).GetMethod();
            string methodName = mb.DeclaringType + "." + mb.Name;

            LogRecord rec = new LogRecord(msg, methodName, logLevel, tag);
            AppendLog(rec);

            if (rec.LogLevel <= ConsoleLogLevel)
                Console.WriteLine(rec.ToConsoleString(IncludeTimeStampInConsole, TimeStampFormat));

            if (rec.LogLevel <= LogFileLogLevel)
            {
                lock (logFileLock)
                {
                    using (StreamWriter sw = (File.Exists(logFileName)) ? File.AppendText(logFileName) : File.CreateText(logFileName))
                    {
                        sw.WriteLine(rec.ToLogFileString(TimeStampFormat));
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
        }
예제 #2
0
 private static void AppendLog(LogRecord log)
 {
     logCount++;
     //not a great implementation but it works. Total waste of ReShuffleing RAM when full and removing
     if (logRecords.Count == logRecords.Capacity)
         logRecords.Remove(0);
     logRecords.Add(log);
 }