Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is passive logging possible in .NET?

I'm frequently frustrated by the amount of logging I have to include in my code and it leads me to wonder if there's a better way of doing things.

I don't know if this has been done or if someone has come up with a better idea but I was wondering is there a way anyone knows of to "inject" a logger into an application such that it passively monitors the thread and quietly logs processes as they occur without having to do things like:

public void MyProcess(int a, string b, object c)
{
  log(
    String.Format(
      "Entering process MyProcess with arguments: [a] = [{0}]; [b] = [{1}]; [c] = [{2}]",
      a.ToString(),
      b,
      c.ToString()
  );

  try
  {
    int d = DoStuff(a)
    log(
      String.Format(
        "DoStuff({0}) returned value {1}",
        a.ToString(),
        d.ToString()
      )
    );
  }
  catch (Exception ex)
  {
    log(
      String.Format("An exception occurred during process DoStuff({0})\nException:\n{1}",
      a.ToString(),
      ex.ToString())
    )
  }
}

What would be great is if I could say to my logger:

Monitor(MyClass.MyMethod)

It would then monitor everything that goes on inside of that method including passed in arguments along with method calls and values passed into those methods, exceptions that occur etc.

Has anyone implemented something like this in the past? Could it even be implemented? Is logging in this fashion just a pipe dream?

I'd love to design something that would do this, but I just don't even know where I'd begin. Of course, I don't want to reinvent the wheel either, if it's already been done, it would be great if someone could point me in the right direction.

Any suggestions would be gratefully received...

Edit: I thought I'd comment on an answer which queried as to the level of detail required in the log. It is frequently required that configurable levels of logging be provided such that if the configuration specifies detailed logging then everything is logged, whereas if critical logging is configured then only certain information is logged along with exceptions. If fatal logging is configured then only information which causes the application to die would be logged. Would something like this be configurable or would AOP require 3 or 4 different builds depending on the number of logging levels?

I frequently use 4 levels: Fatal, Critical, Information, Detailed

like image 567
BobTheBuilder Avatar asked Mar 26 '09 22:03

BobTheBuilder


4 Answers

You can use PostSharp to log "around" the method. This is exactly the kind of thing AOP is good at. You might want to start with Log4PostSharp - a plugin specifically for logging.

like image 166
Jon Skeet Avatar answered Oct 26 '22 02:10

Jon Skeet


This is the classic example from Aspect Oriented Programming. See PostSharp for a very good CLR-based library.

like image 38
Daniel Earwicker Avatar answered Oct 26 '22 02:10

Daniel Earwicker


This is one of the textbook (not sure which textbook has AoP in it but you get the idea) examples of AoP - logging : where you want to stick something before and after a method.

You might want to explore the AoP route, PostSharp is one of the popular ones, along with Microsoft Unity (IoC too), Castle.

One simplistic example of AoP is you add your code before and after methods, instead of adding the method calls inside the actual methods. As you've tagged the question with C# you might want to just look into making an extension method to log it, which is already in this question.

I would take a practical approach: how much actual logging are you doing? Can you get away with just an extension method instead of razzle-dazzle'ing the person reading your code. The logging built into the .NET framework is decent already.

like image 32
Chris S Avatar answered Oct 26 '22 02:10

Chris S


In addition to the logging methods mentioned by Jon, its probably also worth noting another useful feature in VS for tracing program flow, and that is the ability to have non-breaking breakpoints that will simply output a message or run a macro when hit (note you can print variable values as well)

Right click on your breakpoint and choose the When Hit... context menu item.

And of course one other very useful feature is the Trace object and Trace Listners in System.Diagnostics.

like image 44
Tim Jarvis Avatar answered Oct 26 '22 00:10

Tim Jarvis