Note that there are some explanatory texts on larger screens.

plurals
  1. POStuck trying to get Log4Net to work with Dependency Injection
    primarykey
    data
    text
    <p>I've got a simple winform test app i'm using to try some Log4Net Dependency Injection stuff.</p> <p>I've made a simple interface in my <em>Services</em> project :-</p> <pre><code>public interface ILogging { void Debug(string message); // snip the other's. } </code></pre> <p>Then my concrete type will be using Log4Net...</p> <pre><code>public class Log4NetLogging : ILogging { private static ILog Log4Net { get { return LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType); } } public void Debug(string message) { if (Log4Net.IsDebugEnabled) { Log4Net.Debug(message); } } } </code></pre> <p>So far so good. Nothing too hard there.</p> <p>Now, in a different project (and therefore namesapce), I try and use this ...</p> <pre><code>public partial class Form1 : Form { public Form1() { FileInfo fileInfo = new FileInfo("Log4Net.config"); log4net.Config.XmlConfigurator.Configure(fileInfo); } private void Foo() { // This would be handled with DI, but i've not set it up // (on the constructor, in this code example). ILogging logging = new Log4NetLogging(); logging.Debug("Test message"); } } </code></pre> <p>Ok .. also pretty simple. I've hardcoded the ILogging instance but that is usually dependency injected via the constructor. Anyways, when i check this line of code...</p> <pre><code>return LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); </code></pre> <p>the <code>DeclaringType</code> type value is of the <em>Service</em> namespace, not the type of the <em>Form</em> (ie. X.Y.Z.Form1) which actually called the method. </p> <p>Without passing the type INTO method as another argument, is there anyway using reflection to figure out the <code>real</code> method that called it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload