Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Create errorlog File in .NET using C#

0.00/5 (No votes)
30 Oct 2014 1  
This tip presents an example of errorlog file creation

Introduction

Errorlog file creation is more important when you are writing some logic in code behind. Here, I simply specify a small example to present the errorlog file creation using C#.

In .NET application, for every request it will call some codebehind method. If any unexpected exception will occur, then you log that information in one file. If any exception occurs in runtime, if you want to know then simply check the errorlog file.

Using the Code

Here, I will give a step by step explanation to create an errorlog file.

Step 1

Create one enum type that mentions logged data is information or exception:

//
// create enum type
//
Public enum MessageType{Information,Exception};

Step 2

Create one class that specifies errorlog class and gets the path where errorlog information is stored.

//
// create Class 
//
Public class ErrorLog{

static string LogFolder=ConfigurationManager.Appsettings["LogFolder"];
static string LogFilename="";
}

Note: Here, I am maintaining LogFolder information in web.config to be dynamic.

Step 3

Create one method that is common for log information or exception:

//
// create Class 
//
Public class ErrorLog{

static string LogFolder=ConfigurationManager.Appsettings["LogFolder"];
static string LogFilename="";


Private static void LogMessage(strin Message,MessageType messageType)
  {
  strin filename=Datetime.now.Tostring("ddMMyyyy")+"_log.txt";
  if(!System.IO.Directory.Exists(LogFolder))
      {
        System.IO.Directory.CreateDirectory(Logfolder);

      }
   LogFilename=LogFolder+filename;
   string message=String.Format("\r\n{0}\t{1}\t{2}",Datetime.Now.ToLongTimeString(),messageType.Message);
   System.IO.File.AppendAllText(LogFilename,message);
   }
}

Step 4

Finally, specify the methods for exception and information:

//
// create Class 
//
Public class ErrorLog{

static string LogFolder=ConfigurationManager.Appsettings["LogFolder"];
static string LogFilename="";


Private static void LogMessage(string Message,MessageType messageType)
  {
  strin filename=Datetime.now.Tostring("ddMMyyyy")+"_log.txt";
  if(!System.IO.Directory.Exists(LogFolder))
      {
        System.IO.Directory.CreateDirectory(Logfolder);
      }
   LogFilename=LogFolder+filename;
   string message=String.Format("\r\n{0}\t{1}\t{2}",Datetime.Now.ToLongTimeString(),messageType.Message);
   System.IO.File.AppendAllText(LogFilename,message);

   }

   Public static void LogError(string Message)
     {
      LogMessage(Message,MessageType.Exception);
     }

  Public Static void LogInfo(string Message)
     {
      LogMessage(Message,MessageType.Information);
     }
}

That's it. Your errorlog file is created. Now wherever you required to log exception, you simply call methods.

//
// To log Information call LogInfo method
//

ErrorLog.LogInfo("Begin: Home- Index - get action: ")

//your action code come ere

ErrorLog.LogInfo("End: Home- Index - get action :");

Finally

In ASP.NET MVC, the calling code is like this:

//
// To log Information call LogInfo method
//

Public ActionResult Index(){

      try{

       ErrorLog.LogInfo("Begin: Home- Index - get action: ")

        //your action code come ere

        return View();       
        }
       catch(exception ex)
        {
        ErrorLog.LogError("Exception: Home- Index - get action  "+ ex.Message)
        }

       finally{
        ErrorLog.LogInfo("End: Home- Index- get action:")
       }
}

I hope everyone understands. Try it your own way. It will be more useful.

Points of Interest

This is more useful when an unexpected exception will occur and we log that information and we check it.

History

  • 2014-10-30

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here