How to Log Exception in a file?

32,170

Solution 1

Others have posted some good links on getting the Logging Application Block (LAB) working so I won't duplicate that here.

In terms of formatting your exception you have 3 choices that I can think of:

  1. Use the default Exception.ToString() implementation (it's not bad)
  2. Write a Custom Formatter that integrates into the LAB.
  3. Write a helper function that performs the formatting and passes the string into the Write method.

If option 1 doesn't meet your needs then I would recommend going with option 3 (since option 2 is overkill).

A simple example would be something like:

    catch (Exception exception)
    {
        Logger.Write(LogHelper.CreateExceptionString(exception));
    }

    ...

    public static string CreateExceptionString(Exception e)
    {
        StringBuilder sb = new StringBuilder();
        CreateExceptionString(sb, e, String.Empty);

        return sb.ToString();
    }

    private static void CreateExceptionString(StringBuilder sb, Exception e, string indent)
    {
        if (indent == null)
        {
            indent = String.Empty;
        }
        else if (indent.Length > 0)
        {
            sb.AppendFormat("{0}Inner ", indent);
        }

        sb.AppendFormat("Exception Found:\n{0}Type: {1}", indent, e.GetType().FullName);
        sb.AppendFormat("\n{0}Message: {1}", indent, e.Message);
        sb.AppendFormat("\n{0}Source: {1}", indent, e.Source);
        sb.AppendFormat("\n{0}Stacktrace: {1}", indent, e.StackTrace);

        if (e.InnerException != null)
        {
            sb.Append("\n");
            CreateExceptionString(sb, e.InnerException, indent + "  ");
        }
    }

Solution 2

Microsoft has provided extensive guidance on this here: Developing Applications Using the Logging Application Block

It's well worth familiarizing yourself with what they have to say, as it's quite powerful.

If you want something a little more down to earth, some working examples are provided in this blog article: How To Configure and Use the Logging Application Block

If you're still having trouble after reading those, edit your post with more specific details about what seems to be the problem.

Solution 3

@iSid Is this what you are asking for

public static void printException(Exception ex){
                Console.WriteLine("HelpLink = {0}", ex.HelpLink);
                Console.WriteLine("Message = {0}", ex.Message);
                Console.WriteLine("Source = {0}", ex.Source);
                Console.WriteLine("StackTrace = {0}", ex.StackTrace);
                Console.WriteLine("TargetSite = {0}", ex.TargetSite);

            }

Solution 4

You need to add an appropriate listener for Text files - see this post, its for ASP.NET but the config files work the same way across applications.

Share:
32,170
IsmailS
Author by

IsmailS

HTML5, JavaScript, BackboneJS, AngularJS, jQuery, REST, OpenRasta, ASP.NET, ASP.NET MVC, WCF developer, SDET, C#, VB.NET Agile Techinical Lead Like swimming, reading non-fiction/motivational http://toolsforwebdevelopers.blogspot.com

Updated on May 02, 2020

Comments

  • IsmailS
    IsmailS about 4 years

    I want to be able to do logging in every catch block. Something like this.

    catch (Exception exception)
    {
      Logger.Write(exception);
    }
    

    and then the settings in the configuration will pick up the Message and StackTrace property etc using customer listener.

    I would like to use Enterprise Library Logging Application Block. I'm sure someone must have done this already.