• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

ASP.net -- C# -- i need to write all the exceptions into a logfile.can anybody tell me in detail about this with some code samples.

i      am working a C# 2008 project. here i need to write all exceptions/errors to a log file.i don't have any idea about the concept or code.so pls get me brief detail with code sample.
0
monachan
Asked:
monachan
  • 2
2 Solutions
 
DrunkenELFCommented:
Dear Monachan,
Try this console application code to write exceptions in to a log file.

Cheers!
D-ELF


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace ConsoleApplication1
{
class errorlogs
{
 
String filepath = "C:/Documents and Settings/error/log.txt"; //path of errorlog file
 
static void Main(string[] args)
{
errorlogs er = new errorlogs();
object i = new IOException();
er.writelog(i); //passing the ioexception
}
 
public void writelog(object a){
 
try{
StreamWriter logWriter;
if(File.Exists(filepath)){
logWriter = File.AppendText(filepath);
}
else{
logWriter = File.CreateText(filepath);
 
}
logWriter.WriteLine();
logWriter.WriteLine("------"+ DateTime.Now +"--------");
logWriter.WriteLine();
logWriter.WriteLine(a);
logWriter.WriteLine();
logWriter.WriteLine("--------------------------------");
logWriter.Close();
 
}
catch(Exception e){
Console.Out.WriteLine("Error in writing errorlog:" +e.Message); 
}
}
 
}
} 

Open in new window

0
 
DrunkenELFCommented:

Dear Monachan,

U can try the below code also

Cheers!
D-ELF

using System.Diagnostics;  // Include this namespace first  #region "Global Varialble Goes here"            
string EventLogName = "FlyingCowApplicationLog";            
// Put Convenient Name here ..may be Application specific..!! #endregion 
#region "Private Methods Goes here"         
// as for now this method is throwing error.... You need to call this method into any of your page's event for testing purpose !!        private void ErrorMethodGoesHere()        
{
            try
            {
                // Suupose Code given here throws some error
                throw new Exception("Some Exception Goes here");
            }
            catch (Exception ex)
            {
                SaveToEventLog(ex.Message.ToString());
                // Do other stuff like Emailing to Concerned Person about Error occurance ;-)
            }
            finally
             {
                // Free All the Occupied resources here !!
            }
        }
        // This method is used to Save error message to Event Log !!!
        private void SaveToEventLog(string _message)      
{
            try
            {
                //Create our own log if it not already exists
                if (!EventLog.SourceExists(this.EventLogName))
                    EventLog.CreateEventSource(this.EventLogName, this.EventLogName);
                EventLog MyLog = new EventLog();                MyLog.Source = this.EventLogName;
                MyLog.WriteEntry(_message, EventLogEntryType.Warning);
            }
            catch(Exception e)
            {
                e.Message.ToString(); // to check here if there is any Exception or not !!!
            }        
}
#endregion
// That's All !!!

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now