Skip to content

Commit 58c1815

Browse files
committed
Add sample app that produces some logs
1 parent 860d43e commit 58c1815

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

samples/Sample/AppDbContext.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace Sample;
6+
7+
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
8+
{
9+
public DbSet<Employee> Employees { get; set; }
10+
}
11+
12+
public class Employee
13+
{
14+
public int Id { get; init; }
15+
16+
[MaxLength(200)]
17+
public required string Name { get; init; }
18+
19+
public required DateOnly DateOfBirth { get; init; }
20+
}

samples/Sample/Program.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using Microsoft.Data.Sqlite;
3+
using Microsoft.EntityFrameworkCore;
4+
using Sample;
5+
using Serilog;
6+
using Serilog.Extensions.Logging;
7+
using Serilog.Formatting.Log4Net;
8+
9+
var formatter = new Log4NetTextFormatter(options => options.UseCDataMode(CDataMode.IfNeeded).UseNoXmlNamespace());
10+
var logger = new LoggerConfiguration()
11+
.MinimumLevel.Debug()
12+
.WriteTo.Console()
13+
.WriteTo.File(formatter, "logs.xml")
14+
.CreateLogger();
15+
16+
logger.Information("Running on .NET {DotnetVersion:l}", Environment.Version);
17+
18+
using var sqliteConnection = new SqliteConnection("Data Source=:memory:");
19+
sqliteConnection.Open();
20+
using var loggerFactory = new SerilogLoggerFactory(logger);
21+
22+
var options = new DbContextOptionsBuilder<AppDbContext>()
23+
.UseLoggerFactory(loggerFactory)
24+
.EnableSensitiveDataLogging()
25+
.UseSqlite(sqliteConnection)
26+
.Options;
27+
28+
using var context = new AppDbContext(options);
29+
context.Database.EnsureCreated();
30+
context.Employees.Add(new Employee { Name = "Ric Weiland", DateOfBirth = new DateOnly(1953, 4, 21) });
31+
context.SaveChanges();

samples/Sample/Sample.csproj

+20
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>

serilog-formatting-log4net.sln

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Formatting.Log4Net"
1717
EndProject
1818
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Formatting.Log4Net.Tests", "tests\Serilog.Formatting.Log4Net.Tests.csproj", "{E94ABEEA-1E45-42DE-9752-896974FC4389}"
1919
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample", "samples\Sample\Sample.csproj", "{C43E237A-E30E-4839-8D43-F1528A518209}"
21+
EndProject
2022
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{7AA372D2-2CE3-417F-ADA5-FD96743FCAB1}"
2123
ProjectSection(SolutionItems) = preProject
2224
.github\workflows\continuous-integration.yml = .github\workflows\continuous-integration.yml
@@ -37,6 +39,10 @@ Global
3739
{E94ABEEA-1E45-42DE-9752-896974FC4389}.Debug|Any CPU.Build.0 = Debug|Any CPU
3840
{E94ABEEA-1E45-42DE-9752-896974FC4389}.Release|Any CPU.ActiveCfg = Release|Any CPU
3941
{E94ABEEA-1E45-42DE-9752-896974FC4389}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{C43E237A-E30E-4839-8D43-F1528A518209}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43+
{C43E237A-E30E-4839-8D43-F1528A518209}.Debug|Any CPU.Build.0 = Debug|Any CPU
44+
{C43E237A-E30E-4839-8D43-F1528A518209}.Release|Any CPU.ActiveCfg = Release|Any CPU
45+
{C43E237A-E30E-4839-8D43-F1528A518209}.Release|Any CPU.Build.0 = Release|Any CPU
4046
EndGlobalSection
4147
GlobalSection(NestedProjects) = preSolution
4248
{7AA372D2-2CE3-417F-ADA5-FD96743FCAB1} = {F0707B5E-7B54-4D36-993B-17293615DEF7}

0 commit comments

Comments
 (0)