Click here to Skip to main content
15,881,862 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, how can I make a log file to record all the activities from my aplication?
Posted

You can custom implement , dump all the info to Text file or use Logging Frameworks like Log4Net.

Have a look into log4net Tutorial[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Nov-13 17:42pm    
Just one little problem: who told you the OP's application is ASP.NET? And you did not even provide a link to original product.
Please see my answer.
—SA
Ranjan.D 4-Nov-13 17:48pm    
I did not mentioned it's for ASP.NET?? Now where comes the ASP.NET?? Log4net applies for Desktop / Web Applications.

If OP is interested with Log4NET , I will let him/her to go ahead and research.
Sergey Alexandrovich Kryukov 4-Nov-13 18:14pm    
You are right. My mistake. Voted 5 to credit you answering first, will fix my answer accordingly.
—SA
Ranjan.D 4-Nov-13 19:31pm    
Thanks for understanding. As always your solution is really awesome.
Sergey Alexandrovich Kryukov 4-Nov-13 20:35pm    
Thank you very much.
—SA
In nearly all cases, use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Please see also my past answer on log customization. This is not as trivial as it may seem:
How to create event log under a folder[^],
MsBuild OutPut to the TextBox on the fly in Windows Application[^].

As to the use of 3rd-party (Apache) open-source log4net (please see Solution 1), it also can be useful. Please see:
http://en.wikipedia.org/wiki/Log4net#Ports[^],
http://logging.apache.org/log4net/[^].

In most cases, there is no a need to use any 3rd-party or self-baked methods or frameworks.

—SA
 
Share this answer
 
v3
public static class Logger
{
public static void Log(
string message)
{
File.AppendAllText(
“C:\\MyLogFile.txt” ,
DateTime.Now.
ToShortDateString() + ” ” +
DateTime.Now.
ToShortTimeString() + “:\t”
+ message + Environment.
NewLine);
}
}

static void Main( string[]
args)
{
Application.
ThreadException += new
System.Threading.
ThreadExceptionEventHandler(
Application_ThreadException
); //it must be before
Application.Run

Application.Run();
}
static void Application_
ThreadException( object
sender, System.Threading.
ThreadExceptionEventArgs e)
{
Logger.Log(e.Exception.
ToString());
}
 
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