File Logger : Log « Development Class « C# / C Sharp






File Logger

     

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading;
using System.IO;
using System.Threading.Tasks;

namespace Raona.Util.Log
{
    public class FileLogger
    {
        #region Instance Fields

        private static BlockingCollection<string> logMessageQueue = new BlockingCollection<string>();
        private static string logFilePath = null;
        private static CancellationToken cancellationToken = CancellationToken.None;
        private static Task loggerTask = null;

        #endregion

        #region Class Constructor (Public)

        static FileLogger()
        {
            FileLogger.loggerTask = Task.Factory.StartNew(() => FileLogger.WriteMessages());
        }

        #endregion

        #region Class Properties (Public)

        public static string LogFilePath
        {
            get
            {
                return FileLogger.logFilePath;
            }
            set
            {
                FileLogger.logFilePath = value;
            }
        }

        public static CancellationToken CancellationToken
        {
            get
            {
                return FileLogger.cancellationToken;
            }
            set
            {
                FileLogger.cancellationToken = value;
            }
        }

        #endregion

        #region Class Methods (Public)

        public static void Log(string format, params object[] arguments)
        {
            FileLogger.logMessageQueue.Add(string.Format(format, arguments));
        }

        #endregion

        #region Class Methods (Private)

        private static void WriteMessages()
        {
            foreach (string message in logMessageQueue.GetConsumingEnumerable(FileLogger.cancellationToken))
            {
                using (FileStream file = new FileStream(FileLogger.logFilePath, FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter writer = new StreamWriter(file))
                    {
                        writer.WriteLine("[{0}] {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"), message);
                    }
                }
            }
        }

        #endregion
    }
}

   
    
    
    
    
  








Related examples in the same category

1.Demonstrates registering and event source and writing to the Application LogDemonstrates registering and event source and writing to the Application Log
2.Add a EventLogTraceListener to the listener collection and writing error messages to the Application log
3.Test Log
4.Set Text Output To Event LogSet Text Output To Event Log
5.Log utility based on File with File.AppendAllText
6.Log message with StreamWriter
7.Log error to a file
8.Logger
9.Log Utility
10.Log Exception with Event Log
11.Logger class
12.Logger 2
13.Instantiates a Trace log for detailed tracking of an application's internal activities.
14.Your own Logger
15.Implements a TextWriter for writing information to the debugger log.