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:
Public enum MessageType{Information,Exception};
Step 2
Create one class that specifies errorlog class and gets the path where errorlog information is stored.
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:
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:
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.
ErrorLog.LogInfo("Begin: Home- Index - get action: ")
ErrorLog.LogInfo("End: Home- Index - get action :");
Finally
In ASP.NET MVC, the calling code is like this:
Public ActionResult Index(){
try{
ErrorLog.LogInfo("Begin: Home- Index - get action: ")
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