Closed risogolo closed 7 years ago
That should work.
NLog isn't even aware it's a controller class.
So then it does not in my case, my setup is as recommended
please post the full config and the startup.cs
and the version numbers of the NLog packages
NLog.Web.AspNetCore 4.3.1 either 4.4.1
public Startup(IHostingEnvironment env)
{
env.ConfigureNLog("NLog.config");
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddNLog();
app.AddNLogWeb();
Please post your NLog.Config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogToConsole="true" internalLogLevel="Warn">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets async="true">
<target name="Console" xsi:type="ColoredConsole" useDefaultRowHighlightingRules="true" layout="${date:universalTime=true:format=yyyy-MM-dd HH\:mm\:ss.fff} | ${aspnet-TraceIdentifier:padding=-37:fixedLength=true} | ${level:uppercase=true:padding=-5:fixedLength=true} | ${callsite:includeNamespace=false:includeSourcePath=false:padding=-40} | ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}" />
<target name="File" xsi:type="File" fileName="nlog-all.log" layout="${date:universalTime=true:format=yyyy-MM-dd HH\:mm\:ss.fff} | ${aspnet-TraceIdentifier:padding=-37:fixedLength=true} | ${level:uppercase=true:padding=-5:fixedLength=true} | ${callsite:includeNamespace=false:includeSourcePath=false:padding=-40} | ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}" />
<target xsi:type="Null" name="blackhole" />
</targets>
<rules>
<logger name="Microsoft.*" maxLevel="Info" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Info" writeTo="Console" />
<logger name="*" minlevel="Debug" writeTo="File" />
</rules>
</nlog>
all the stuff works with this configuration if I don't use constructor injection and use ILogger _logger = LogManager.GetCurrentClassLogger();
instead
I have managed it work, but it logs only INFO severity messages
Are you testing with a file or console?
Sounds like a NLog config issue
file and console = same results, config file is above, but as I said if I use private field
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
everything works fine, but I want to inject Logger via constructor Microsoft.Extensions.Logging.ILogger<Class>
- this does not log properly
, but I want to inject Logger via constructor Microsoft.Extensions.Logging.ILogger
- this does not log properly
I think that there is a mistake how your class is created. Do you use new
? NLog hasn't any influence on the DI system.
but in this example https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(csproj---vs2017)
section 5. Write logs looks like. I don't want to use new
In my case the logger is normally injected via constructor
Is it possible to extends the example application to reproduce the issue? (this example https://github.com/NLog/NLog.Extensions.Logging/tree/master/examples/aspnet-core-example-vs2017)
Unfortunately I don't have time for that.
So, to be honest this entire thing was my fault, I cloned the example project as you recommended and everything worked as expected. So the things was that I'm using separated ASP.NET Core pipelines based on this example https://www.strathweb.com/2017/04/running-multiple-independent-asp-net-core-pipelines-side-by-side-in-the-same-application/
and the example lacks the support for logging, so I needed to add support for it by calling ConfigureLogging(loggerFactory)
which I had to call on WebHostBuilder, now it looks likeIWebHost webHost = new WebHostBuilder().UseKestrel().ConfigureServices(servicesConfiguration).ConfigureLogging(loggerFactory).UseStartup<EmptyStartup>().Build();
and now it works as expected, so I think we may close the issue.
Thanks for the feedback! Glad it has been resolved.
I have used this setup
https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(csproj---vs2017)
but, it seems it works only in controllers, if I want use the same approach in different class I need to useprivate readonly ILogger _logger = LogManager.GetCurrentClassLogger();
otherwise messages are not logged at all.