serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging
Apache License 2.0
312 stars 101 forks source link

Logging Issue in ASP.NET Core Web Api #73

Closed dataspring closed 7 years ago

dataspring commented 7 years ago

In asp.net core 1.1.1, using the serilog-extensions-logging, doesn't log and create log files under rollingfile sink but creates a file but doesn't log under file sink. Somehow the LitearteConsole is working. Strange though, any solution appreciated...my initialization code under Startup.cs is as follows: `

        Configuration = builder.Build();

        string logLocation = Configuration.GetValue<String>("LogLocation:Path") + "log-{Date}.txt";
        string logFileLocation = Configuration.GetValue<String>("LogLocation:Path") + "log.txt";
        //string logLocation = Path.Combine(env.ContentRootPath, "log-{Date}.txt");

        var logger = new LoggerConfiguration()
                        .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                        .MinimumLevel.Override("System", LogEventLevel.Warning)
                        .WriteTo.LiterateConsole()
                        .WriteTo.File(path: logFileLocation, fileSizeLimitBytes: 10737418) //100 MB
                        //.WriteTo.RollingFile(pathFormat: logLocation, retainedFileCountLimit:7, fileSizeLimitBytes:10737418) //100 MB
                        .CreateLogger();

Code under Configure:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
                    IApplicationLifetime appLifetime)
    {
        //loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        loggerFactory.AddSerilog();
        appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);

appsettings value for logLocation: "LogLocation": {"Path": "C:\Bootstrap-api\Logs\"} Serilog.Extensions.Logging ver 1.4.0

nblumhardt commented 7 years ago

Thank you for the report.

Are you targeting .NET Core, or .NET 4.6?

If 4.6, can you please try adding the following to you .CSPROJ file?

    <PackageReference Include="system.net.http" Version="4.3.1" />

(This may be the same issue as: https://github.com/serilog/serilog-sinks-seq/issues/57 if so.)

Best regards, Nick

dataspring commented 7 years ago

I'm targeting .NET Core, i.e. .NETCoreApp 1.. I'm not sure I want to add that .NET 4.6 dll in this, as this app may be deployed in linux

dataspring commented 7 years ago

Here's my csproj file

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
  </PropertyGroup>
  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Dapper" Version="1.50.2" />
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="1.4.0" />
    <PackageReference Include="Serilog.Sinks.File" Version="3.2.0" />
    <PackageReference Include="Serilog.Sinks.Literate" Version="2.1.0" />
    <PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0-rc3" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\CoreEncryption\CoreEncryption.csproj" />
  </ItemGroup>
</Project>
nblumhardt commented 7 years ago

Thanks for the follow-up.

<PackageReference Include="system.net.http" Version="4.3.1" /> is not a .NET Framework-only package (it's also available on .NET Core), but the reason I susggested it seems to only apply to .NET Framework projects. Since you're targeting the netcoreapp1.1 TFM this probably isn't relevant to your case.

C:\Bootstrap-api\Logs\ will require admin permissions to write to, unless you've overridden them manually for the folder. Is the app running under a user account with enough permissions to write files there?

Setting 'SelfLog.Enable(Console.Out)might shed some light on it (you may need to temporarily removeLiterateConsole` so that you can see the output).

nblumhardt commented 7 years ago

Closing, hope you found a solution to this - please let us know if you think there's still an issue here.