CodeProject - Redirect LINQ to SQL DataContext Log Messages To Log4Net
"An article that shows how one can use Log4Net to capture the log messages that are generated by the LINQ to SQL DataContext classes
Visual Studio automatically generates the subclasses of
DataContext
class when dealing with LINQ to SQL for ORM. These auto generated classes have a property namedLog
of typeSystem.IO.TextWriter
. If an object of typeTextWriter
or its subclass is assigned to this property, then it will use that instance for writing the log messages. As Log4Net is one of the most used Logging libraries for .NET enterprise level projects, in this project I will present a solution to capture the log messages through Log4Net logging.Background
In my project, I have a well established logging framework that uses Log4Net. Now that I moved to LINQ to SQL, I needed to get the raw SQL that is generated from the LINQ C# codes for both instrumentation and performance purposes. From MSDN, I came to know that I need to pass an instance of type
TextWriter
to get the log messages from theDataContext
subclasses. So, I ended up writing a subclass ofTextWriter
calledLINQLogger
, which encapsulates the responsibility of using Log4Net logger under the hood as it receives the log messages from theDataContext
classes...."
I like the architecture of this article, with its abstracting out of the logging engine, usage of patterns, etc.
No comments:
Post a Comment
NOTE: Anonymous Commenting has been turned off for a while... The comment spammers are just killing me...
ALL comments are moderated. I will review every comment before it will appear on the blog.
Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...
I reserve, and will use, the right to not approve ANY comment for ANY reason. I will not usually, but if it's off topic, spam (or even close to spam-like), inflammatory, mean, etc, etc, well... then...
Please see my comment policy for more information if you are interested.
Thanks,
Greg
PS. I am proactively moderating comments. Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...