Save Error Log or Exception Log in Text File in C# - Cybarlab
Save Error Log or Exception Log in Text File in C# - Cybarlab
Cybarlab
Home Programming Framework Database Scripting HTML/CSS
19 Mar 2013
Search this site...
Home ASP.NET C# Save Error Log or Exception Log in text file Search
in C#
CRUD operations in
article:
http://cybarlab.com/save-error-log-in-text-file-in-c-sharp 1/6
8/9/2017 Save Error Log or Exception Log in text file in C# | Cybarlab
It is better to save error logs in text file rather than database. Because
text file responds much quicker then database.
<appSettings>
<add key="LogDirectory" value="D:\LogFiles\"/>
</appSettings>
using System.Configuration;
using System.Text;
using System.IO;
lock (typeof(ErrorLog))
{
StreamWriter oStreamWriter = null;
try
{
oStreamWriter = new StreamWriter(LogDirectory, true);
oStreamWriter.WriteLine(logLine);
Status = true;
}
catch
{
http://cybarlab.com/save-error-log-in-text-file-in-c-sharp 2/6
8/9/2017 Save Error Log or Exception Log in text file in C# | Cybarlab
}
finally
{
if (oStreamWriter != null)
{
oStreamWriter.Close();
}
}
}
return Status;
}
If any error occurred during program execution time its logs will be saved
in a text file with date and time in LogDirectory.
Related Posts
Comments
By Syed Ayaz
http://cybarlab.com/save-error-log-in-text-file-in-c-sharp 4/6
8/9/2017 Save Error Log or Exception Log in text file in C# | Cybarlab
By Rashed
Syed Ayaz,
It will work for both desktop or web application.
on November 17, 2013 Reply
By dinesh
Code is not working
on November 14, 2013 Reply
By Rashed
Dineh,
Can your check now. I think it will work now.
Thanks for comments.
on November 17, 2013 Reply
By Vijayalakshmi
Thank you very much for this post. It is really helpful.
God Bless You with Good Health, Long Life, Properity,
Happiness, Wisodm and Peace.
on August 29, 2014 Reply
By Vijayalakshmi
Thank you very much for this post. It is really helpful.
God Bless You with Good Health, Long Life, Prosperity,
Happiness, Wisdom and Peace.
on August 29, 2014 Reply
By Srj
Thanks for the post. Great work amigo. Code works like
a charm
on October 20, 2014 Reply
Leave a Reply
Comment
http://cybarlab.com/save-error-log-in-text-file-in-c-sharp 5/6
8/9/2017 Save Error Log or Exception Log in text file in C# | Cybarlab
Name
Website
Post Comment
C# API
ASP.NET Interview
About
Contact
Sitemap
Terms of Use
Privacy Policy
http://cybarlab.com/save-error-log-in-text-file-in-c-sharp 6/6