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.
monachanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.