1

We have a C# ORM module that generates queries. It logs generated queries and other information into the error/trace file. It is used by our web application. Most of our queries are generated dynamically (based on dynamic business rules and user interactions) which we have little control over.

Obviously the ORM module is completely decoupled from the web application, and therefore completely unaware of web sessions, page hits etc. The downside is that the log entries it generates cannot be traced back to the original page hit. When we notice a non-performant query in the log, we cannot easily determine which page hit generates that query.

How can I keep the ORM module decoupled from the web application, but still allow it to log enough relevant information for me to correlate each log entry to a session/page hit?

(I know we can pass the logger object around, and the logger object can preserve that session information. However, some of these function calls are nested 10 levels deep, so passing the logger around is cumbersome).

2 Answers 2

2

You can use a nested diagnostic context (NDC) to push the information in before you enter the ORM module (or at whatever level is appropriate).

This idea is referred to in the log4net documentation as Context Stacks but most loggers have a similar concept.

4
  • Interesting. If the assumption is that each page hit is serviced by a unique thread, we can push the page parameters via ThreadContext to uniquely identify it. I wonder if that assumption is true. Commented Mar 20, 2014 at 18:29
  • Yes, that would be similar to what using the NDC does, but it wouldn't be supported natively by the logger. The beauty of using NDC is that you can manage it with the log formatting string. Commented Mar 20, 2014 at 18:33
  • For the benefits of people like me (I had to look it up), NDC means Nested Diagnostic Context for log4net. Commented Mar 20, 2014 at 20:25
  • Thanks, I've edited the answer to name that abbreviation. Commented Mar 20, 2014 at 21:34
0

Make the logger globally available, so you don't have to pass it around. If you have more than one logger (maybe one per session), you need to make it "globally within the session context". For example, you could provide a global "logger repository" which holds a reference to each active logger and lets you access them by the session id, or some other context id. The one thing you need here is some kind of id which is available through all the layers of your application. It is hard to give you a more specific advice without actually knowing more about your code.

1
  • 2
    A global logger would not have the session/page hit information necessary to correlate the log entries. Commented Mar 20, 2014 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.