title: How to inject functionality into a C# class without modifying it
body: <p>I want to create a Nuget package that will enable database logging in Entity Framework without actually modifying the existing codebase I'm working with. Is there a way to do that?</p>
  
  <p>Basically, the problem I want to solve is this: I want to make a database logging tool for my company, but I don't want to have to check in any code to the codebase. It would be far easier (e.g. I wouldn't have to convince to create the logging tool or debate that the performance hit is worth it) if I could just optionally slip in the code when I wanted to. Later, people could adopt it if they wanted to, or choose not to. </p>
  
  <p>I mean, of course I could always just have a boolean flag to choose whether or not to log or not, but again, that's touching some core code that might raise some flags with people. </p>
  
  <p>Here's an example of code I want to "slip in":</p>
  
  <pre><code>    public DbContext(Func&lt;IIdentity&gt; getUser) : base(getUser)
      {
          // How do I inject this into the constructor?
          this.Database.Log = s =&gt;
          {
              if (!string.IsNullOrWhiteSpace(s))
              {
                  CustomEventLogger.Append(s);
              }
          };
          // end injected code
      }
  </code></pre>