---
# System prepended metadata

title: .NET Core Microsoft標準のロギングサンプル 2021-07-16
tags: [.NET Core, C#, Microsoft.Extensions.Logging, ロギング]

---

---
lang: ja-jp
breaks: true
---
# .NET Core Microsoft標準のロギングサンプル 2021-07-16

> dotnet/AspNetCore.Docs
> https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/logging/index/samples/3.x/LoggingConsoleApp

ファイルに出力する機能は用意されていない模様。

### LoggingConsoleApp.csproj

```xml=
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="3.0.0" />
  </ItemGroup>

</Project>
```

### Program.cs

```csharp=
using Microsoft.Extensions.Logging;

#region snippet_LoggerFactory
class Program
{
    static void Main(string[] args)
    {
        using var loggerFactory = LoggerFactory.Create(builder =>
        {
            builder
                .AddFilter("Microsoft", LogLevel.Warning)
                .AddFilter("System", LogLevel.Warning)
                .AddFilter("LoggingConsoleApp.Program", LogLevel.Debug)
                .AddConsole()
                .AddEventLog();
        });
        ILogger logger = loggerFactory.CreateLogger<Program>();
        logger.LogInformation("Example log message");
    }
}
#endregion
```

## 実行結果

![](https://i.imgur.com/Z2lFvK4.png)


![](https://i.imgur.com/l9dJurf.png)



###### tags: `.NET Core` `Microsoft.Extensions.Logging` `ロギング` `C#`
