Search code examples
c#unity-containertracesource

Injecting System.Diagnostics.Trace with Unity


I need to inject a TraceSource in my application as a Dependancy Injection. How can I achieve to do that since TraceSource does not implements any interface?


Solution

  • Wrap an instance of the TraceSource class in another class that does implement an Interface. Delegate all calls to the wrapper to the real TraceSource.

    I don't see any other way to do this with unity. The small benefit you'd get with a wrapper is you'd have a point where you could swap out logging to a different library, which sometimes happens when there are new developers on the team or who have replaced the team and they prefer a different logger.