With the below code you can log the error messages getting in your application into text files.
By doing this we can easily trace out the issue where we are getting.
Add the below code into Web.config App settings section
Create class file as shown below
By doing this we can easily trace out the issue where we are getting.
Add the below code into Web.config App settings section
<add key="LogFilePath" value="C:\RootFolder\SubFolder\Logs\LogFile"/>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace CreateLogFiles
{
public class CreateLogFiles
{
private string sLogFormat;
private string sErrorTime;
public CreateLogFiles()
{
//sLogFormat
used to create log files format :
//
dd/mm/yyyy hh:mm:ss AM/PM ==> Log Message
sLogFormat = DateTime.Now.ToShortDateString().ToString()
+ " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
//this
variable used to create log filename format "
//for
example filename : ErrorLogYYYYMMDD
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
sErrorTime = sYear + sMonth + sDay;
}
public void ErrorLog(string sPathName, string sErrMsg)
{
StreamWriter sw = new StreamWriter(sPathName + sErrorTime, true);
sw.WriteLine(sLogFormat + sErrMsg);
sw.Flush();
sw.Close();
}
}
}
Create object for this class and pass the error message to it as shown below
string LogFilePath = ConfigurationSettings.AppSettings["LogFilePath"];
CreateLogFiles Err = new CreateLogFiles();
try
{
Err.ErrorLog(@LogFilePath, "Method called");
MethodName();
Err.ErrorLog(@LogFilePath, "Method completed");
}
catch (Exception ex)
{
Err.ErrorLog(@LogFilePath, "Main Import Load Catch exception");
Err.ErrorLog(@LogFilePath,
ex.Message);
}
Now you can see the text file will be created in the specified log file path folder.

0 comments:
Post a Comment