LuJoSoft Error Log class

Will be posting article on how to create simple application, with full working code in C#
And welcome to anyone who want to post their own article
Post Reply
User avatar

Topic author
Superl The Boss
Site Admin
Site Admin
Posts: 1250
Joined: Sat Apr 16, 2011 7:49 am
Location: Montreal, Canada
Gender:
Contact:

LuJoSoft Error Log class

Post by Superl The Boss » Sat Dec 19, 2015 6:24 pm

LuJoSoft Error Log class

Introduction:
This is the class library that I created to report errors in my application, with this it's easier for me to locate bugs and fix them. Hope that can be useful for any of you.

I will also include for download the compile version of this class "dll" at the end of this topic, you will need to use net framework 4.5 or higher to use it

Here is the complete code for the class:

Code: Select all

// ***********************************************************************
// Assembly         : LJErrorLog
// Author           : Luc Joly
// Created          : 12-10-2015
//
// Last Modified By : Luc Joly
// Last Modified On : 12-19-2015
// ***********************************************************************
// <copyright file="ErrorLog.cs" company="LuJoSoft">
// Any distribution of source code by others without approval of Luc Joly is prohibited.
// </copyright>
// <summary>
// This file contains the ErrorLog  class.
// </summary>
// ***********************************************************************
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace LJErrorLog
{
    /// <summary>
    /// Class ErrorLog. This class cannot be inherited.
    /// </summary>
    public sealed class ErrorLog
    {
        #region Properties

        /// <summary>
        /// The _ log path
        /// </summary>
        private string _LogPath;

        /// <summary>
        /// Gets the log path.
        /// </summary>
        /// <value>The log path.</value>
        public string LogPath
        {
            get
            {
                return _LogPath;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorLog" /> class.
        /// </summary>
        public ErrorLog()
        {
            _LogPath = Path.Combine(Path.Combine(Application.StartupPath), "Errors");
            if (!Directory.Exists(_LogPath))
                Directory.CreateDirectory(_LogPath);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorLog" /> class.
        /// </summary>
        /// <param name="logPath">The log path.</param>

        public ErrorLog(string logPath)
        {
            _LogPath = logPath;
            if (!Directory.Exists(_LogPath))
                Directory.CreateDirectory(_LogPath);
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Logs exception information to the assigned log file.
        /// </summary>
        /// <param name="exception">Exception to log.</param>
        /// <returns>System.String.</returns>
        public string LogError(Exception exception)
        {
            Assembly caller = Assembly.GetEntryAssembly();
            Process thisProcess = Process.GetCurrentProcess();
            //writer = File.AppendText("ErrorLog.txt");

            string LogFile = "ErrorLog.txt";

            //            if (!File.Exists(Path.Combine(_LogPath, LogFile)))
            //            {
            //                using (StreamWriter sw = new StreamWriter(Path.Combine(_LogPath, LogFile))
            //)
            //                {
            //                }
            //            }
            //StreamWriter writer;
            //writer = File.AppendText(Path.Combine(_LogPath, LogFile));
            using (StreamWriter sw = new StreamWriter(Path.Combine(_LogPath, LogFile), true))
            {
                sw.WriteLine("==============================================================================");
                sw.WriteLine(caller.FullName);
                sw.WriteLine("------------------------------------------------------------------------------");
                sw.WriteLine("Application Information");
                sw.WriteLine("------------------------------------------------------------------------------");
                sw.WriteLine("Program      : " + caller.Location);
                sw.WriteLine("Time         : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                sw.WriteLine("User         : " + Environment.UserName);
                sw.WriteLine("Computer     : " + Environment.MachineName);
                sw.WriteLine("OS           : " + Environment.OSVersion.ToString());
                sw.WriteLine("Culture      : " + CultureInfo.CurrentCulture.Name);
                sw.WriteLine("Processors   : " + Environment.ProcessorCount);
                sw.WriteLine("Working Set  : " + Environment.WorkingSet);
                sw.WriteLine("Framework    : " + Environment.Version);
                sw.WriteLine("Run Time     : " + (DateTime.Now - Process.GetCurrentProcess().StartTime).ToString());
                sw.WriteLine("------------------------------------------------------------------------------");
                sw.WriteLine("Exception Information");
                sw.WriteLine("------------------------------------------------------------------------------");
                sw.WriteLine("Source       : " + exception.Source.ToString().Trim());
                sw.WriteLine("Method       : " + exception.TargetSite.Name.ToString());
                sw.WriteLine("Type         : " + exception.GetType().ToString());
                sw.WriteLine("Error        : " + GetExceptionStack(exception));
                sw.WriteLine("Stack Trace  : " + exception.StackTrace.ToString().Trim());
                sw.WriteLine("------------------------------------------------------------------------------");
                sw.WriteLine("Loaded Modules");
                sw.WriteLine("------------------------------------------------------------------------------");
                foreach (ProcessModule module in thisProcess.Modules)
                {
                    try
                    {
                        sw.WriteLine(module.FileName + " | " + module.FileVersionInfo.FileVersion + " | " + module.ModuleMemorySize);
                    }
                    catch (FileNotFoundException)
                    {
                        sw.WriteLine("File Not Found: " + module.ToString());
                    }
                    catch (Exception)
                    {

                    }
                }
                sw.WriteLine("------------------------------------------------------------------------------");
                sw.WriteLine(LogFile);
                sw.WriteLine("==============================================================================");
                sw.WriteLine();
                sw.WriteLine();
                sw.WriteLine();
            }

            return LogFile;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets the exception stack.
        /// </summary>
        /// <param name="e">The e.</param>
        /// <returns>System.String.</returns>
        private string GetExceptionStack(Exception e)
        {
            StringBuilder message = new StringBuilder();
            message.Append(e.Message);
            while (e.InnerException != null)
            {
                e = e.InnerException;
                message.Append(Environment.NewLine);
                message.Append(e.Message);
            }

            return message.ToString();
        }

        #endregion
    }
}


Now to use it you just need to call it in your application this way

Code: Select all

using LJErrorLog;

public static ErrorLog Logger = new ErrorLog();

try
 {
        Do some work.....
   ......
   ......
 }
 catch (Exception ex)
 {
         Logger.LogError(ex);
 }



LJErrorLog.dll
LJErrorLog.dll
(8 KiB) Downloaded 60 times
LJErrorLog.dll
LJErrorLog.dll
(8 KiB) Downloaded 60 times


Come and say hello in here
Any donation will help click here please.

Have a nice day :103:

Post Reply

Return to “Coding Forum”

Who is online

Users browsing this forum: No registered users and 1 guest