Skip to content

Commit 437e4c0

Browse files
committed
[WIP] Samples
1 parent c84d13e commit 437e4c0

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.4" />
11+
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.1" />
12+
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
13+
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\..\src\Serilog.Formatting.Log4Net.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
using Microsoft.Data.Sqlite;
4+
using Microsoft.EntityFrameworkCore;
5+
using Serilog;
6+
using Serilog.Extensions.Logging;
7+
using Serilog.Formatting.Log4Net;
8+
9+
var logger = new LoggerConfiguration()
10+
.MinimumLevel.Debug()
11+
.WriteTo.Console()
12+
.WriteTo.File(new Log4NetTextFormatter(), "logs.xml")
13+
.CreateLogger();
14+
15+
logger.Information("Running on .NET {DotnetVersion:l}", Environment.Version);
16+
17+
using var sqliteConnection = new SqliteConnection("Data Source=:memory:");
18+
sqliteConnection.Open();
19+
20+
var options = new DbContextOptionsBuilder<BloggingContext>()
21+
.UseLoggerFactory(new SerilogLoggerFactory(logger))
22+
.EnableSensitiveDataLogging()
23+
.UseSqlite(sqliteConnection)
24+
.Options;
25+
26+
using var context = new BloggingContext(options);
27+
context.Database.EnsureCreated();
28+
context.Employees.Add(new Employee { Name = "Ric Weiland", DateOfBirth = new DateOnly(1953, 4, 21) });
29+
context.SaveChanges();
30+
31+
public class BloggingContext(DbContextOptions<BloggingContext> options) : DbContext(options)
32+
{
33+
public DbSet<Employee> Employees { get; set; }
34+
}
35+
36+
public class Employee
37+
{
38+
public int Id { get; init; }
39+
40+
[MaxLength(200)]
41+
public required string Name { get; init; }
42+
43+
public required DateOnly DateOfBirth { get; init; }
44+
}

serilog-formatting-log4net.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
2323
.github\workflows\test-report.yml = .github\workflows\test-report.yml
2424
EndProjectSection
2525
EndProject
26+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{A12D8BA3-5CFD-43EC-8CC1-C1F9028D75D3}"
27+
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Log4NetFormattingSample", "samples\Log4NetFormattingSample\Log4NetFormattingSample.csproj", "{63F130BF-8FF3-45A6-B957-B2504B261491}"
29+
EndProject
2630
Global
2731
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2832
Debug|Any CPU = Debug|Any CPU
@@ -37,8 +41,13 @@ Global
3741
{E94ABEEA-1E45-42DE-9752-896974FC4389}.Debug|Any CPU.Build.0 = Debug|Any CPU
3842
{E94ABEEA-1E45-42DE-9752-896974FC4389}.Release|Any CPU.ActiveCfg = Release|Any CPU
3943
{E94ABEEA-1E45-42DE-9752-896974FC4389}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{63F130BF-8FF3-45A6-B957-B2504B261491}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{63F130BF-8FF3-45A6-B957-B2504B261491}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{63F130BF-8FF3-45A6-B957-B2504B261491}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{63F130BF-8FF3-45A6-B957-B2504B261491}.Release|Any CPU.Build.0 = Release|Any CPU
4048
EndGlobalSection
4149
GlobalSection(NestedProjects) = preSolution
4250
{7AA372D2-2CE3-417F-ADA5-FD96743FCAB1} = {F0707B5E-7B54-4D36-993B-17293615DEF7}
51+
{63F130BF-8FF3-45A6-B957-B2504B261491} = {A12D8BA3-5CFD-43EC-8CC1-C1F9028D75D3}
4352
EndGlobalSection
4453
EndGlobal

0 commit comments

Comments
 (0)