2019-01-05 09:03:10 +08:00
|
|
|
---
|
|
|
|
title: Migrate from Microsoft.Extensions.Logging 2.1 to 2.2 or 3.0
|
2022-02-14 21:12:27 +08:00
|
|
|
author: rick-anderson
|
2019-01-05 09:03:10 +08:00
|
|
|
description: Learn how to migrate a non-ASP.NET Core application that uses Microsoft.Extensions.Logging from 2.1 to 2.2 or 3.0.
|
2022-02-14 21:12:27 +08:00
|
|
|
ms.author: riande
|
2019-01-05 09:03:10 +08:00
|
|
|
ms.custom: mvc
|
|
|
|
ms.date: 01/04/2019
|
|
|
|
uid: migration/logging-nonaspnetcore
|
|
|
|
---
|
|
|
|
|
|
|
|
# Migrate from Microsoft.Extensions.Logging 2.1 to 2.2 or 3.0
|
|
|
|
|
|
|
|
This article outlines the common steps for migrating a non-ASP.NET Core application that uses `Microsoft.Extensions.Logging` from 2.1 to 2.2 or 3.0.
|
|
|
|
|
|
|
|
## 2.1 to 2.2
|
|
|
|
|
|
|
|
Manually create `ServiceCollection` and call `AddLogging`.
|
|
|
|
|
|
|
|
2.1 example:
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
using (var loggerFactory = new LoggerFactory())
|
|
|
|
{
|
|
|
|
loggerFactory.AddConsole();
|
|
|
|
|
|
|
|
// use loggerFactory
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
2.2 example:
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
var serviceCollection = new ServiceCollection();
|
|
|
|
serviceCollection.AddLogging(builder => builder.AddConsole());
|
|
|
|
|
|
|
|
using (var serviceProvider = serviceCollection.BuildServiceProvider())
|
|
|
|
using (var loggerFactory = serviceProvider.GetService<ILoggerFactory>())
|
|
|
|
{
|
|
|
|
// use loggerFactory
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## 2.1 to 3.0
|
|
|
|
|
|
|
|
In 3.0, use `LoggingFactory.Create`.
|
|
|
|
|
|
|
|
2.1 example:
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
using (var loggerFactory = new LoggerFactory())
|
|
|
|
{
|
|
|
|
loggerFactory.AddConsole();
|
|
|
|
|
|
|
|
// use loggerFactory
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
3.0 example:
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
using (var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()))
|
|
|
|
{
|
|
|
|
// use loggerFactory
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Additional resources
|
|
|
|
|
2020-06-02 09:18:17 +08:00
|
|
|
* [Microsoft.Extensions.Logging.Console NuGet package](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console/).
|
|
|
|
* <xref:fundamentals/logging/index>
|