title: Register NLog target (with dependencies) with ASP.NET Core's DI
body: <p>I'm using the latest NLog, ASP.NET Core, EF Core.</p>
  
  <p>I wrote a custom log target which saves via EF:</p>
  
  <pre><code>public  class MyEFTarget : TargetWithLayout
  {
      private readonly IMyContext _context;
  
      public MyEFTarget(IMyContext context) : base()
      {
          _context = context;
      }
  
      protected override void Write(LogEventInfo logEvent)
      {
          // and so on...
      }
  
  }
  </code></pre>
  
  <p>My <code>Startup</code>:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
    // register context with DI (as scoped)
    services.AddDbContext&lt;MyContext&gt;(o =&gt; o.UseSqlite(config.GetConnectionString("Default")));
    services.AddScoped&lt;IMyContext, MyContext&gt;();      
  
    // target depends on context, so must also be registered with DI
    // I chose scoped so it's the same as the context
    services.AddScoped&lt;MyEFTarget&gt;();
  
    // ...and so on
  }
  
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env,
      ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
  {
    // normal NLog config 
    env.ConfigureNLog("NLog.config");
    loggerFactory.AddNLog();
    app.AddNLogWeb();
  
    // register target
    Target.Register&lt;MyEFTarget&gt;("MyEFTarget");
  
    // add target + rule programmatically
    var target = serviceProvider.GetService&lt;MyEFTarget&gt;();
    var rule = new LoggingRule("*", LogLevel.Info, target);
    LogManager.Configuration.AddTarget("MyEFTarget", target);    // problem is here ********
    LogManager.Configuration.LoggingRules.Add(rule);
    LogManager.ReconfigExistingLoggers();
  
    // ...and so on
  }
  </code></pre>
  
  <p>The problem is that NLog caches the <code>target</code> instance for the duration of the app...I assume? So it's basically a singleton. And it's keeping a reference to my EF context, which might be disposed.</p>
  
  <p>Is there a better way?</p>
  
  <p><em>To restate the problem generically (because it's not an EF issue), "how do I register/add a custom target which has short-lived dependencies"?</em></p>