1

I'm looking for a way to have the my code automatically log errors or warnings in c# with the param name & value and the Action being attempted on it. I am willing to take the hit to performance for this. Currently the only way I can think of doing this is by not calling any method directly rather calling it via a executor (extension) method that would accept a expression/func. I would prefer however to not have to litter my code by doing this. Is there any way to do this?

Thank you

Alex
  • 167
  • 1
  • 6
  • 1
    Look at aspect-oriented programming tools like [PostSharp](http://www.postsharp.net/) – D Stanley Sep 04 '14 at 20:34
  • PostSharp keeps popping up in articles I'm reading for other stuff. I feel kinda stupid because logging is one of the examples given on their website. Thanks! – Alex Sep 04 '14 at 20:51
  • @alex: what tech are you targeting? Windows Client(WPF, Winforms), web (asp.net, asp.mvc). Please tag your question with the appropriate tags. – bic Sep 04 '14 at 21:06
  • Have you tried to log all method calls and their arguments? I doubt you are willing to take that performance hit. If you have more than a few thousand lines of code your application will come to a sudden halt due to the overhead of tracing every method call and its arguments. For a real world application you need to be much more selective what to trace. Postsharp works but allocates proxy objects for the method arguments even when tracing is off. This showed up as bottleneck in my profiler. I keep telling them to fix this by getting rid of postsharp tracing. – Alois Kraus Sep 04 '14 at 21:12
  • @AloisKraus Interesting, its a Regular c# Compiled project. I would probably want to do 2 dif compilations one with interceptors and one without so if speed becomes a issue I can survive. – Alex Sep 04 '14 at 21:25
  • You can check out Unity Interception for logging if you plan to use some dependency injection framework (some links http://stackoverflow.com/questions/11186918/how-to-configure-a-logging-interceptor-for-all-registered-type-in-unity) – Alexei Levenkov Sep 04 '14 at 21:49

1 Answers1

3

Have you tried using the AppDomain.FirstChanceException from this question, or do you need more information other than Exceptions?

Also, Log4Net is apparently good. I've heard it before, but never used it. It is alos from this question.

Community
  • 1
  • 1
Dakotah Hicock
  • 380
  • 2
  • 15
  • Actually didn't know bout that, but I would like more. Ideally i would like to Log any instance where the default of the return type is returned. Also it would be great to specify the logging level on the method either by method or class attribute – Alex Sep 04 '14 at 20:37
  • Can you explain what you mean a bit more? Like if you are expecting a `string`, you'd like a log of every time `null` or `String.Empty` is returned? – Dakotah Hicock Sep 04 '14 at 20:38
  • Ok so for example if I called class Cook(ingredient) without passing in ingredients (passed in as null or whatnot) so in general with ingredients a IEnumerable would be passed back in this case I would like to log the fact that ingredients was null as "param Ingredients has null value -- returning null" – Alex Sep 04 '14 at 20:44
  • This would be a simple case i would like it to work even if the operation is a lambda exp – Alex Sep 04 '14 at 20:45
  • 1
    I am not aware of anything you can do to log this globally. You would have to set up catches for this for each function you would like to record, as far as I know. – Dakotah Hicock Sep 04 '14 at 20:47
  • I'm actually using log4net right now I want to avoid having to even place Logging comments down – Alex Sep 04 '14 at 20:48