NLog / NLog.Web

NLog integration for ASP.NET & ASP.NET Core 2-8
https://nlog-project.org
BSD 3-Clause "New" or "Revised" License
320 stars 166 forks source link

Unable to cast object of type 'Microsoft.AspNetCore.Hosting.Internal.WebHost' to type 'Microsoft.AspNetCore.Hosting.IWebHostBuilder' error #329

Closed etiennejcharles closed 5 years ago

etiennejcharles commented 6 years ago

Hello guys,

I'm a little new to C#, but when trying to use your package with

<PackageReference Include="NLog.Web.AspNetCore" Version="4.7.0" />
<PackageReference Include="NLog" Version="4.5.10" />

I had to change lines of the documentation you gave from

BuildWebHost(args).Run();

To

CreateWebHostBuilder(args).Build().Run();

Since the CreateWebHostBuilder also already Build()'s the solution.

I had to remove

WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .ConfigureLogging(logging => {
        logging.ClearProviders();
        logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
    })
    .UseNLog()
    .Build();

The part where it says

Build()

Don't know if that makes sens, if so Il do a pull request to update the documentation.

Instead of using the following,

NLog version: 4.5.10

Platform: .NET Core 2 - MacOS 10.13.6

Current NLog config (xml or C#, if relevant)

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="info"
      internalLogFile="${basedir}\Logs\internal-nlog.txt">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <!-- write logs to file  -->
    <target xsi:type="File" name="allfile" fileName="${basedir}\Logs\nlog-all-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

    <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
    <target xsi:type="File" name="ownFile-web" fileName="${basedir}\Logs\nlog-own-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" maxLevel="Info" final="true" /> <!-- BlackHole without writeTo -->
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  </rules>
snakefoot commented 6 years ago

Microsoft changed their recommendation for NetCore2.1.

See also https://github.com/NLog/NLog.Web/issues/300#issuecomment-404949937

I guess a new wiki-page should be created for NetCore2.1

304NotModified commented 6 years ago

I guess a new wiki-page should be created for NetCore2.1

Or add a note for .NET Core 2.1?