Log Utility : Log « Development Class « C# / C Sharp






Log Utility

      

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Reflection;

namespace MCServerHelper
{
    internal static class LogUtility
    {
        private static readonly object syncObject = new object();
        private static string logPath = string.Empty;
        private static void CreateLogFile()
        {
            // not thread safe, just for test
            if (string.IsNullOrEmpty(logPath))
            {
                // Create a log file
                DirectoryInfo pathInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + Path.DirectorySeparatorChar + "MCServerHelper");
                if (!pathInfo.Exists)
                {
                    pathInfo.Create();
                }

                FileInfo logFile = new FileInfo(pathInfo.FullName + Path.DirectorySeparatorChar + "logfile.txt");
                if (!logFile.Exists)
                {
                    logFile.Create();
                }

                logPath = logFile.FullName;
            }
        }

        public static void LogException(Exception ex)
        {
            CreateLogFile();

            if (!string.IsNullOrEmpty(logPath))
            {
                using (StreamWriter writer = new StreamWriter(logPath, true))
                {
                    writer.WriteLine(DateTime.Now.ToString());
                    writer.WriteLine(ex.Message);
                    writer.WriteLine(ex.Source);
                    writer.WriteLine(ex.TargetSite);
                    writer.WriteLine(ex.StackTrace);

                    StackTrace trace = new StackTrace(ex);
                    if (trace.FrameCount > 0)
                    {
                        StackFrame frame = trace.GetFrame(0);
                        if (frame != null)
                        {
                            MethodBase method = frame.GetMethod();
                            writer.WriteLine(string.Format("{0}, Line {1}", method.Name, frame.GetFileLineNumber()));
                        }
                    }

                    writer.WriteLine();
                }

                if (ex.InnerException != null)
                {
                    LogException(ex.InnerException);
                }
            }
        }

        public static void LogMessage(string message)
        {
            CreateLogFile();

            if (!string.IsNullOrEmpty(logPath))
            {
                using (FileStream stream = new FileStream(logPath, FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
                    {
                        writer.WriteLine(DateTime.Now.ToString());
                        writer.WriteLine(message);
                        writer.WriteLine("");
                    }
                }
            }
        }
    }
}

   
    
    
    
    
    
  








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.File Logger
8.Log error to a file
9.Logger
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.