Serilog Eventlog问题

时间:2018-01-30 06:15:51

标签: c# serilog

我有asp.net核心2.0项目。 我正在使用Serilog进行日志记录。 这是我的项目文件:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <UserSecretsId>aspnet-IdentityServerWithAspNetIdentity-04C6939F-E672-4E56-B4A5-5F064EB67F23</UserSecretsId>
    <StartupObject></StartupObject>
    <RootNamespace>Pci.Services.IdentityServer4.Core</RootNamespace>
    <AssemblyName>Pci.Services.IdentityServer4.Core</AssemblyName>
  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="Services\DbClientStore.cs" />
  </ItemGroup>

  <ItemGroup>
    <None Remove="Certificates\EPTokenCert.pfx" />
    <None Remove="Certificates\idsrv3test.pfx" />
    <None Remove="Certificates\SomeCert.pfx" />
    <None Remove="ServiceConfig.yaml" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="Certificates\EPTokenCert.pfx" />
    <Content Include="Certificates\SomeCert.pfx" />
    <Content Include="ServiceConfig.yaml">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>


  <ItemGroup>
    <PackageReference Include="IdentityServer4.AspNetIdentity" Version="2.1.0" />
    <PackageReference Include="IdentityServer4.EntityFramework" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" PrivateAssets="All" />
    <PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="2.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.2" PrivateAssets="All" />
    <PackageReference Include="Pci.ConfigProvider" Version="2.0.0.13" />
    <PackageReference Include="Serilog" Version="2.6.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="2.1.0" />
    <PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
    <PackageReference Include="Serilog.Enrichers.Process" Version="2.0.1" />
    <PackageReference Include="Serilog.Enrichers.Thread" Version="3.0.0" />
    <PackageReference Include="Serilog.Exceptions" Version="3.0.0" />
    <PackageReference Include="Serilog.Formatting.Compact" Version="1.0.0" />
    <PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
    <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
    <PackageReference Include="Serilog.Sinks.EventLog" Version="3.0.47" />
    <PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
    <PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.1.1" />
    <PackageReference Include="Smi.Encryption" Version="1.0.0" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.4.1" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.1.5" />
    <PackageReference Include="System.IO" Version="4.3.0" />
    <PackageReference Include="System.Security.Permissions" Version="4.4.1" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Include="Certificates\idsrv3test.pfx" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\IdentityServer.Models\IdentityServer.Models.csproj" />
    <ProjectReference Include="..\IdentityServer4.Services\Pci.Services.IdentityServer4.Services.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="System">
      <HintPath>..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

C#logic

Log.Logger = new LoggerConfiguration()
             .MinimumLevel.ControlledBy(_loggingLevelSwitch)
            //.MinimumLevel.Debug()
            //.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            //.MinimumLevel.Override("System", LogEventLevel.Warning)
            //.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
            .Enrich.WithProperty("ApplicationName", _systemConfiguration.ApplicationFriendlyName)
             //.Enrich.FromLogContext()
             .Enrich.WithEnvironmentUserName()
               .Enrich.WithExceptionDetails()
               .Enrich.WithMachineName()
               .Enrich.WithProcessId()
               .Enrich.WithThreadId()
               .WriteTo.ColoredConsole()
            //.WriteTo.File(@"PCI.identityserver4_log.txt")
            .WriteTo.EventLog(GetExecutingAssemblyName(), "Application", restrictedToMinimumLevel: _systemConfiguration.LoggingConfiguration.WindowsEventlogVerbosity)
            //.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Literate)
            .CreateLogger();

.WriteTo.EventLog()正在抛出

  

System.TypeLoadException:&#39;无法加载类型   &#39; System.Diagnostics.EventLog&#39;来自assembly&#39; System,Version = 4.0.0.0,   Culture = neutral,PublicKeyToken = b77a5c561934e089&#39;。&#39;

Nuget包Serilog.Sinks.EventLog(3.0.47)显示以下警告

  

Package&#39; Serilog.Sinks.EventLog 3.0.47&#39;使用&#39; .NETFramework,Version = v4.6.1&#39;进行了恢复。而不是项目目标框架&#39; .NETCoreApp,Version = v2.0&#39;。此软件包可能与您的项目不完全兼容。

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我知道这有点晚了。但是,您使用的Serilog是与.NET core不兼容的。确保已安装Serilog.ASPNetCore软件包。那应该可以解决您的问题。