Skip to content

Commit b5e34d3

Browse files
authored
Adds Microsoft.Extensions.Logging provider (#25)
* no more netfx * Adds Microsoft.Extensions.Logging provider * Make provider part of FsLibLog * Update Serilog example * Docs on getting started with other providers
1 parent 036fc8c commit b5e34d3

File tree

15 files changed

+402
-83
lines changed

15 files changed

+402
-83
lines changed

FsLibLog.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "SerilogExample", "examples\
2525
EndProject
2626
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "SomeLib", "examples\SomeLib\SomeLib.fsproj", "{B4E33887-2856-4809-B185-A80C9B093427}"
2727
EndProject
28+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "MicrosoftLoggingExample", "examples\MicrosoftLoggingExample\MicrosoftLoggingExample.fsproj", "{49CFCA86-7F76-4361-80A3-F67EF8763D9B}"
29+
EndProject
2830
Global
2931
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3032
Debug|Any CPU = Debug|Any CPU
@@ -122,6 +124,18 @@ Global
122124
{B4E33887-2856-4809-B185-A80C9B093427}.Release|x64.Build.0 = Release|Any CPU
123125
{B4E33887-2856-4809-B185-A80C9B093427}.Release|x86.ActiveCfg = Release|Any CPU
124126
{B4E33887-2856-4809-B185-A80C9B093427}.Release|x86.Build.0 = Release|Any CPU
127+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
128+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Debug|Any CPU.Build.0 = Debug|Any CPU
129+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Debug|x64.ActiveCfg = Debug|Any CPU
130+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Debug|x64.Build.0 = Debug|Any CPU
131+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Debug|x86.ActiveCfg = Debug|Any CPU
132+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Debug|x86.Build.0 = Debug|Any CPU
133+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Release|Any CPU.ActiveCfg = Release|Any CPU
134+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Release|Any CPU.Build.0 = Release|Any CPU
135+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Release|x64.ActiveCfg = Release|Any CPU
136+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Release|x64.Build.0 = Release|Any CPU
137+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Release|x86.ActiveCfg = Release|Any CPU
138+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B}.Release|x86.Build.0 = Release|Any CPU
125139
EndGlobalSection
126140
GlobalSection(NestedProjects) = preSolution
127141
{5D30E174-2538-47AC-8443-318C8C5DC2C9} = {C397A34C-84F1-49E7-AEBC-2F9F2B196216}
@@ -131,5 +145,6 @@ Global
131145
{BDF041F1-D693-4BCE-9741-C9606E901437} = {9BAA7731-9F1B-4B89-8AD8-DB546D7E816D}
132146
{916E3A28-4E3D-4931-A13B-9B56E0B66B88} = {9BAA7731-9F1B-4B89-8AD8-DB546D7E816D}
133147
{B4E33887-2856-4809-B185-A80C9B093427} = {9BAA7731-9F1B-4B89-8AD8-DB546D7E816D}
148+
{49CFCA86-7F76-4361-80A3-F67EF8763D9B} = {9BAA7731-9F1B-4B89-8AD8-DB546D7E816D}
134149
EndGlobalSection
135150
EndGlobal

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Target.create "Replace" <| fun _ ->
4747
(!! "paket-files/TheAngryByrd/FsLibLog/src/FsLibLog/FsLibLog.fs")
4848
```
4949

50+
### 3. [Setup a LogProvider](#log-providers)
51+
5052
## Using in your library
5153

5254
### Open namespaces
@@ -199,14 +201,58 @@ Providers are the actual logging framework that sends the logs to some destinati
199201
### Currently supported provider
200202

201203
- [Serilog](https://github.com/serilog/serilog)
204+
- [Microsoft.Extensions.Logging](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0)
205+
206+
#### Setting up Serilog
207+
208+
1. Install [Serilog](https://www.nuget.org/packages/Serilog)
209+
2. Install [Serilog.Sinks.ColoredConsole](https://www.nuget.org/packages/Serilog.Sinks.Console/) (or any other [Sink](https://github.com/serilog/serilog/wiki/Provided-Sinks))
210+
3. Create your `Logger`
211+
212+
```fsharp
213+
let log =
214+
LoggerConfiguration()
215+
.MinimumLevel.Verbose()
216+
.WriteTo.Console(outputTemplate= "{Timestamp:o} [{Level}] <{SourceContext}> ({Name:l}) {Message:j} - {Properties:j}{NewLine}{Exception}")
217+
.Enrich.FromLogContext()
218+
.CreateLogger();
219+
Log.Logger <- log
220+
```
221+
222+
4. FsLibLog will pick up Serilog automatically, no need to tell FsLibLog about it
223+
224+
### Setting up Microsoft.Extensions.Logging
225+
226+
1. Install [Microsoft.Extensions.Logging](https://www.nuget.org/packages/Microsoft.Extensions.Logging/)
227+
2. Install [Microsoft.Extensions.Logging.Console](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console) (or any other [Provider](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0#logging-providers-1))
228+
3. Create your `ILoggerFactory`
229+
230+
```fsharp
231+
let microsoftLoggerFactory = LoggerFactory.Create(fun builder ->
232+
builder
233+
.SetMinimumLevel(LogLevel.Debug)
234+
.AddSimpleConsole(fun opts -> opts.IncludeScopes <-true)
235+
// .AddJsonConsole(fun opts -> opts.IncludeScopes <- true)
236+
|> ignore
237+
238+
)
239+
```
240+
241+
4. Tell FsLibLog to use this factory
242+
243+
```fsharp
244+
FsLibLog.Providers.MicrosoftExtensionsLoggingProvider.setMicrosoftLoggerFactory microsoftLoggerFactory
245+
```
246+
247+
1. One downside to this is you need to do this for every library your application consumes that uses FsLiblog.
202248
203-
### Custom Providers
249+
#### Custom Providers
204250
205251
You can implement and teach FsLibLog about your own custom provider if one is not listed. You have to do 2 things:
206252
207253
1. You have to implement the `ILogProvider` interface. [Example Implemenation](https://github.com/TheAngryByrd/FsLibLog/blob/master/examples/ConsoleExample/Program.fs#L5-L90)
208254
2. You have to tell FsLibLog to use it. [Example calling FsLibLog.LogProvider.setLoggerProvider](https://github.com/TheAngryByrd/FsLibLog/blob/master/examples/ConsoleExample/Program.fs#L94)
209-
a. One downside to this is you need to do this for every library your application consumes that uses FsLiblog.
255+
1. One downside to this is you need to do this for every library your application consumes that uses FsLiblog.
210256
211257
---
212258

adapters/FsLibLog.Adapters.Marten/FsLibLog.Adapters.Marten.fsproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
</PropertyGroup>
77
<PropertyGroup>
@@ -29,6 +29,5 @@
2929
<ItemGroup>
3030
<ProjectReference Include="..\..\src\FsLibLog\FsLibLog.fsproj" />
3131
</ItemGroup>
32-
<Import Project="..\..\netfx.props" />
3332
<Import Project="..\..\.paket\Paket.Restore.targets" />
3433
</Project>

adapters/FsLibLog.Adapters.Npgsql/FsLibLog.Adapters.Npgsql.fsproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
</PropertyGroup>
77
<PropertyGroup>
@@ -29,6 +29,5 @@
2929
<ItemGroup>
3030
<ProjectReference Include="..\..\src\FsLibLog\FsLibLog.fsproj" />
3131
</ItemGroup>
32-
<Import Project="..\..\netfx.props" />
3332
<Import Project="..\..\.paket\Paket.Restore.targets" />
3433
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
<WarnOn>3390;$(WarnOn)</WarnOn>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<!-- <Compile Include="..\..\src\providers\FsLibLog.Providers.Microsoft.Extensions.Logging\FsLibLog.Providers.Microsoft.Extensions.Logging.fs" /> -->
11+
<Compile Include="Program.fs" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\SomeLib\SomeLib.fsproj" />
16+
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
21+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Learn more about F# at http://docs.microsoft.com/dotnet/fsharp
2+
3+
open System
4+
open Microsoft.Extensions.Logging
5+
6+
7+
[<EntryPoint>]
8+
let main argv =
9+
let microsoftLoggerFactory = LoggerFactory.Create(fun builder ->
10+
builder
11+
.SetMinimumLevel(LogLevel.Debug)
12+
.AddSimpleConsole(fun opts -> opts.IncludeScopes <-true)
13+
// .AddJsonConsole(fun opts -> opts.IncludeScopes <- true)
14+
|> ignore
15+
16+
)
17+
18+
// This line is important to make Microsoft.Extensions.Logging work
19+
FsLibLog.Providers.MicrosoftExtensionsLoggingProvider.setMicrosoftLoggerFactory microsoftLoggerFactory
20+
SomeLib.Say.nestedHello "Howdy" |> printfn "%s"
21+
SomeLib.Say.hello "Whatup" |> printfn "%s"
22+
try
23+
SomeLib.Say.fail "failed"
24+
with e -> ()
25+
Console.ReadLine() |> ignore
26+
0 // return an integer exit code

examples/SerilogExample/Program.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let main argv =
88
let log =
99
LoggerConfiguration()
1010
.MinimumLevel.Verbose()
11-
.WriteTo.ColoredConsole(outputTemplate= "{Timestamp:o} [{Level}] <{SourceContext}> ({Name:l}) {Message:j} - {Properties:j}{NewLine}{Exception}")
11+
.WriteTo.Console(outputTemplate= "{Timestamp:o} [{Level}] <{SourceContext}> ({Name:l}) {Message:j} - {Properties:j}{NewLine}{Exception}")
1212
.Enrich.FromLogContext() //Necessary if you want to use MappedContext
1313
.CreateLogger();
1414
Log.Logger <- log

examples/SerilogExample/SerilogExample.fsproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Serilog" Version="2.8.0" />
14-
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
13+
<PackageReference Include="Serilog" Version="2.10.0" />
14+
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
1515
<ProjectReference Include="..\SomeLib\SomeLib.fsproj" />
1616
</ItemGroup>
1717

fsc.props

Lines changed: 0 additions & 21 deletions
This file was deleted.

netfx.props

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)