locked
Create a Logging Service to write out to separate external file RRS feed

  • Question

  • I hope this isn't too silly a question, but I figure if I don't ask then I could be passing something valuable up.  I have this class library where I'd like to build some kind of Logger that could be called from another method when certain issues arise.  Are there any .Net Framework technologies for this purpose?  I couldn't find anything outside of EventLog for this and I think that's for the literal Windows Event Log.  In this case I'd like the Logger to write out to a separate file associated with the class library whether text, xml, or whatever.  Are there examples for this?  Is this something I need to build from scratch?  What's typical?

    Jason

    Monday, March 18, 2013 8:29 PM

All replies

  • Here's a rather long discussion about what people choose and why they choose. You can write a wrapper to make switching between logging libraries easier.


    Visual C++ MVP


    Monday, March 18, 2013 10:52 PM
  • Sorry, for some reason your link is set to the Google drive.  As a result it causes me to redirect to my Google drive account.

    Jason

    Monday, March 18, 2013 11:59 PM
  • Hi Jason,

    You can make use of Trace class of .net to write your logs in your log files or else you can rely on any logging tool like log4net or make use of microsoft enterprise logging

    for writing logs into your own log files following code snippet can be helpful (In this case I'd like the Logger to write out to a separate file associated with the class library whether text, xml, or whatever)

    private int Logger(Exception exObj)
    {
    using (Stream myFile = 
      File.Exists(“TestFile.txt”) ? File.Open(“TestFile.txt”, FileMode.Append) : File.Create(“TestFile.txt”))
    {
    /* Create a new text writer using the output stream, 
    and add it to * the trace listeners. */
    TextWriterTraceListener myTextListener = 
       new TextWriterTraceListener(myFile);
    Trace.Listeners.Add(myTextListener);
    // Write output to the file.
    Trace.Write(“\r\n Test output \r\n”);
    Trace.Write(exObj);
    Trace.Write(“\r\n Test output \r\n”);
    
    // Flush the output.
    Trace.Flush();
    myFile.Close();
    
    Trace.Listeners.Remove(myTextListener); 
    // this is very important else you may encounter 
    // error wrt file/stream/trace object
    myFile.Dispose();
    }
    return 0;
    }


    Manish Patil http://patilmanishrao.wordpress.com Posting is provided AS IS with no warranties, and confers no rights.

    Manish Patil's Blog

    ↑ Grab this Headline Animator

    Tuesday, March 19, 2013 7:50 AM
  • That's an interesting approach. Is this typical?

    Jason

    Tuesday, March 19, 2013 11:22 AM
  • sorry, link fixed


    Visual C++ MVP

    Tuesday, March 19, 2013 2:14 PM
  • Hi Jason,

    To answer " Is this typical?":

    • If you are single handedly developing then its upto you to decide what to implement and what not to.
    • If you are in team then what standards your team follows based on that you can proceed.

    Only thing that you as an developer should see is to code effectively by leaving minimal loop holes.

    Industry wide people follow or device many approaches. Yes, but majorly on the lines I mentioned.

    If applications are big and complex enough then log4net and utilities like it are best suited since things will become uniform.

    Enjoy coding, don't hesitate :)


    Manish Patil http://patilmanishrao.wordpress.com Posting is provided AS IS with no warranties, and confers no rights.

    Manish Patil's Blog

    ↑ Grab this Headline Animator

    Tuesday, March 19, 2013 2:20 PM
  • Friday, March 22, 2013 11:05 PM