I upgraded a Web API project from ASP.NET Core 1.x to ASP.NET Core 2.0 with very minimal code changes.
When running the WebAPI the command prompt opens up like normal.
However, every single info message is duplicated.
Is this an ASP.NET Core bug or is this an issue on my end after upgrading?
UPDATE:
I was doing the following in the Startup.cs Configure method:
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
When I remove it there are no duplicates. Is this not needed anymore?
What is the difference between IHttpActionResult and IActionresult ? "IActionResult is the new abstraction that should be used in your actions. Since Web API and MVC frameworks have been unified in ASP.NET Core, various IActionResult implementations can handle both traditional API scenarios.".
IHttpActionResult contains a single method, ExecuteAsync, which asynchronously creates an HttpResponseMessage instance. If a controller action returns an IHttpActionResult, Web API calls the ExecuteAsync method to create an HttpResponseMessage. Then it converts the HttpResponseMessage into an HTTP response message.
There are multiple ways to inject logging and other crosscutting concerns in Web API. One way is to create a custom ApiController class, or a base class for all of our controllers, and then override the ExecuteAsync method. Another way is to use a custom action filter.
WebHost.CreateDefaultBuilder
sets up a lot of the conventional stuff for you, to save the same code having to be generated for each individual ASP.NET Core 2 project (as it was in ASP.NET Core 1.x).
You can see the code for WebHost.CreateDefaultBuilder
here. For your particular scenario, if you look a little further down in the source code, you'll see the following:
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
Due to this, you no longer need to add this code yourself. In doing so, it gets added twice and this ends up doing the logging twice.
If you want a more detailed walkthrough of these changes, Andrew Lock has a good write-up of how this works. He also digs into some of the details of how to override some of these defaults, etc. Note that this write-up is based on ASP.NET Core 2 preview 1, but things are more or less the same in the final version.
I was having the same issue: using NLog, all info was duplicated and recorded that way. For me, what solved the problem was to remove the loggerFactory.AddNLog()
from Configure
at Startup.cs
. I'm using Core 2.0.
For that matter, it is possible to clear the logging providers registered by default by CreateDefaultBuilder().
Host.CreateDefaultBuilder(args).ConfigureLogging((hostContext, loggingBuilder) =>
{
loggingBuilder.ClearProviders();
});
So logs won't be outputed twice and you don't have to configure the host yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With