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).
Logging page/session ID in an inner module
c#;dependency injection;logging
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.