Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating Logging Code from C# Objects

Currently I have a custom built static logging class in C# that can be called with the following code:

EventLogger.Log(EventLogger.EventType.Application, string.Format("AddData request from {0}", ipAddress));

When this is called it simply writes to a defined log file specified in a configuration file.

However, being that I have to log many, many events, my code is starting to become hard to read because all of the logging messages.

Is there an established way to more or less separate logging code from objects and methods in a C# class so code doesn't become unruly?

Thank you all in advance for your help as this is something I have been struggling with lately.

like image 351
Jeffrey Kevin Pry Avatar asked Aug 01 '11 13:08

Jeffrey Kevin Pry


2 Answers

I like the AOP Features, that PostSharp offers. In my opinion Loggin is an aspect of any kind of software. Logging isn't the main value an application should provide.

So in my case, PostSharp always was fine. Spring.NET has also an AOP module which could be used to achieve this.

like image 67
Thorsten Hans Avatar answered Oct 17 '22 01:10

Thorsten Hans


The most commonly used technique I have seen employs AOP in one form or another.

PostSharp is one product that does IL weaving as a form of AOP, though not the only way to do AOP in .NET.

like image 4
Oded Avatar answered Oct 17 '22 01:10

Oded