Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hey Guys,
I am working on a Desktop Application (basically it is a Inventory app). I would like to maintain log in txt format to know where application crashed and to maintain other events.
Please suggest best way to create log which is meaningful.
I really appreciate your help.

right now I am creating txt file in catch block.

Thanks!

What I have tried:

I have tried to create log file in c#.
Posted
Updated 10-Mar-16 23:48pm
v2
Comments
Sinisa Hajnal 9-Mar-16 10:02am    
So, you didn't actually try anything? You could use one of any number of free logging libraries. You could write simple filestream class that will write to local disk. You could create database table that will receive your logging errors...any number of ways.

C#
You can find too many links on google that how to create a file and store in a location. And you can use that code in Catch block, so that if you get any exception it will log in that file.

Also you can find lot of things on google to handle error logs in Windows application.


If you want you can use Elmah(Third party tool) to store the logs in database.

Now it's you choice which option you want to use.

Cheers
 
Share this answer
 
TRY...CATCH is the best practice to catch the exception (may be you can use exception.stackTrace for more detail log), but there are many different ways to write and maintain log.
1. stored .txt file at application startup path
2. store log in database, (but if application crash in between many time database connection gets lost and will get only last updated message not exact code line)
3. Stored log in system's application event log

if you look for performance then I may suggest you 'StringBuilder', as it is fast and append all text easily
look at below sample
C#
StringBuilder sb;
...
sb.Append("log something");

...
// flush every 20 seconds as you do it
File.AppendAllText(filePath+"log.txt", sb.ToString());
sb.Clear();

look at below link for more details of log writing
How to write in log Files in C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900