create LogError file(C# Coding) [modified] for WindowsApplication
-
C# Coding WindowsApplication :
- How can I have Log for error in
try catch
with this informations1.Error Name : 2.Form Name: 3.Function Name that error occurred : 4.Date_Time when error occurred :
2) Where I save this log info. I mean shall i save them inXML or word File
?
modified on Thursday, February 4, 2010 4:38 AM
- How can I have Log for error in
-
C# Coding WindowsApplication :
- How can I have Log for error in
try catch
with this informations1.Error Name : 2.Form Name: 3.Function Name that error occurred : 4.Date_Time when error occurred :
2) Where I save this log info. I mean shall i save them inXML or word File
?
modified on Thursday, February 4, 2010 4:38 AM
- How can I have Log for error in
-
U may want to use log4net. It's quite simple. :) A Brief Introduction to the log4net logging library, using C#[^]
I wanna this log for
windowsApplication
?! So Please HELP ! -
I wanna this log for
windowsApplication
?! So Please HELP ! -
C# Coding WindowsApplication :
- How can I have Log for error in
try catch
with this informations1.Error Name : 2.Form Name: 3.Function Name that error occurred : 4.Date_Time when error occurred :
2) Where I save this log info. I mean shall i save them inXML or word File
?
modified on Thursday, February 4, 2010 4:38 AM
You are being helped. log4net is a tool to help the programmer output log statements to a variety of output targets. http://logging.apache.org/log4net/[^] If you want to re invent the wheel you can create a log file, for example, mylogger.logx using the StreamWriter class maybe like this:
public void Log(string Err)
{
using (StreamWriter sw = new StreamWriter("log.txt", true))
{
sw.WriteLine();
sw.WriteLine();
sw.WriteLine("NEW LOG");
sw.WriteLine("-------------------");
sw.WriteLine();
sw.WriteLine();
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
sw.WriteLine();
sw.WriteLine();
sw.WriteLine("-------------------");
sw.WriteLine();
sw.WriteLine(Err);
}
}Then,
jojoba2010 wrote:
Error Name :
assuming you caught the exception in the catch block like this
catch (Exception ex)
{
Log(ex.Message.ToString());
}jojoba2010 wrote:
Form Name:
Log(this.Text);
jojoba2010 wrote:
Function Name that error occurred :
Log(ex.Source);
jojoba2010 wrote:
Date_Time when error occurred :
The Log function can already do that. Read about Exception and see the many things you can know about an exception that has occured!
Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
- How can I have Log for error in
-
You are being helped. log4net is a tool to help the programmer output log statements to a variety of output targets. http://logging.apache.org/log4net/[^] If you want to re invent the wheel you can create a log file, for example, mylogger.logx using the StreamWriter class maybe like this:
public void Log(string Err)
{
using (StreamWriter sw = new StreamWriter("log.txt", true))
{
sw.WriteLine();
sw.WriteLine();
sw.WriteLine("NEW LOG");
sw.WriteLine("-------------------");
sw.WriteLine();
sw.WriteLine();
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
sw.WriteLine();
sw.WriteLine();
sw.WriteLine("-------------------");
sw.WriteLine();
sw.WriteLine(Err);
}
}Then,
jojoba2010 wrote:
Error Name :
assuming you caught the exception in the catch block like this
catch (Exception ex)
{
Log(ex.Message.ToString());
}jojoba2010 wrote:
Form Name:
Log(this.Text);
jojoba2010 wrote:
Function Name that error occurred :
Log(ex.Source);
jojoba2010 wrote:
Date_Time when error occurred :
The Log function can already do that. Read about Exception and see the many things you can know about an exception that has occured!
Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
I want the function name where this error occurred in ! I mean in which this try and catch is ?
-
I want the function name where this error occurred in ! I mean in which this try and catch is ?
Have a look at
Exception.TargetSite
then. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
I want the function name where this error occurred in ! I mean in which this try and catch is ?
u can get the current class and method name like this. Below as a class variable.
private static readonly string _className =
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString();Put below code inside each function that u want to log.
string thisMethod = _className + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()";
then whenever u want to log the info, u can just simply use
logger.info("Error Occurred + thisMethod);
-
C# Coding WindowsApplication :
- How can I have Log for error in
try catch
with this informations1.Error Name : 2.Form Name: 3.Function Name that error occurred : 4.Date_Time when error occurred :
2) Where I save this log info. I mean shall i save them inXML or word File
?
modified on Thursday, February 4, 2010 4:38 AM
jojoba2010 wrote:
- Where I save this log info. I mean shall i save them in XML or word File ?
you can save the data anyways you want it. xml is structured and easier to browse through. text is easy to implement.
jojoba2010 wrote:
- How can I have Log for error in try catch with this informations 1.Error Name : 2.Form Name: 3.Function Name that error occurred : 4.Date_Time when error occurred :
void AddLogEntry(String errorName, String formName, String functionName)
{
DateTime entryTime = DateTime.Now;//depending on how you want to save the entry i.e. xml or plain text
//append the text in the log file with the entry
//search in google on how to add text to a text/xml file.
}or, you can do this:
void AddLogEntry(Exception ex)
{
String errorName = ex.message;
String trace = ex.StackTrace; //stack trace will give you a brief history on why the error occured
DateTime entryTime = DateTime.Now;//depending on how you want to save the entry i.e. xml or plain text
//append the text in the log file with the entry
//search in google on how to add text to a text/xml file.
}hope this helps and good luck :)
- How can I have Log for error in