Picture 1. Sample error message
Picture 2. Sample error log file
This article demonstrates how to create a simple text based log file to log error messages with your own format using a C# class. Based on your needs, this class can modified and edited to create other log files, such as user activity log, user login time log, etc.
This class demonstrates the use of following namespaces:
The namespaces contain methods to create files in directories.
If you have downloaded the source code, you can follow these steps to install the project. You can then build and debug the code with VS.NET.
- Unzip the project source to \inetpub\wwwroot\CreateLogFiles directory
- Give the Logs directory -in the CreateLogFiles directory- full access permission, log files will be created in this directory
- Create new CreateLogFiles virtual site using your IIS MMC snap in, or Internet Services Manager
- If you are not using CreateLogFiles virtual site, edit the file CreateLogFiles.csproj.webinfo and set the
URLPath to your virtual site name
- Double click CreateLogFiles.csproj with VS.NET and build the project to get the dll file in the bin directory
- Open your browser, browse to your project using http, in this case: http://localhost/CreateLogFiles/CreateLogFiles.aspx
- Try to fill the
TextBox, if you have an error, you can see the errorlog was created in the Logs directory
In this project, the main idea is to create a class,
CreateLogFiles, that will create files in a specific directory from the web. This class uses two namespaces:
Before we make a
function to create an error files, we have to declare some variables that will be used in our function. This is an example of
private string sLogFormat;
private string sErrorTime;
sLogFormat = DateTime.Now.ToShortDateString().ToString()+" "+DateTime.Now.ToLongTimeString().ToString()+" ==> ";
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
sErrorTime = sYear+sMonth+sDay;
and this is a
function that will create error files. Please modify this
function for your needs, or you can make other
functions to create other log files :
public void ErrorLog(string sPathName, string sErrMsg)
StreamWriter sw = new StreamWriter(sPathName+sErrorTime,true);
sw.WriteLine(sLogFormat + sErrMsg);
To use this class, I create an example web form called:
CreateLogFiles. A snapshot of this web form is shown above, picture 1. This is used to search a file in our directory. If the file exists, then no error log file is created, but when a file doesn't exist, it will create an error log file with the specific error message.
This is the code behind for this page. I call the
ErrorLog function from our class when the
Search button is clicked.
private void BtnFind_Click(object sender, System.EventArgs e)
StreamReader sr = new StreamReader(this.TxtFilename.Text);
Msg.Visible = true;
Msg.Text = "File "+ this.TxtFilename.Text +" was found";
CreateLogFiles Err = new CreateLogFiles();
Msg.Visible = true;
Msg.Text = "Fatal error : "+ ex.Message + ", please find a complete error at ErrorLog file";
I am using
catch to check automatically whether file exists or not. An error message gets automatically by the
That's it. :-) You can view complete source code in the download file.