Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
using log4net;
using Microsoft.AspNetCore.Mvc;

namespace Log4netAppenders.Controllers
namespace Log4netAppenders.Controllers;

[Route("api/[controller]")]
[ApiController]
public class LogTestController : ControllerBase
{
[Route("api/[controller]")]
[ApiController]
public class LogTestController : ControllerBase
{
private readonly ILog _logger;
private readonly ILog _logger;

public LogTestController(ILog logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public LogTestController(ILog logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

[HttpGet]
public IActionResult Get()
{
_logger.Debug("This is a debug log.");
_logger.Info("This is an informational log.");
_logger.Warn("This is a warning log.");
_logger.Error("This is an error log.");
[HttpGet]
public IActionResult Get()
{
_logger.Debug("This is a debug log.");
_logger.Info("This is an informational log.");
_logger.Warn("This is a warning log.");
_logger.Error("This is an error log.");

return Ok("Test success!");
}
return Ok("Test success!");
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="log4net.Ext.Json" Version="2.0.10.1" />
<PackageReference Include="MicroKnights.Log4NetAdoNetAppender" Version="2.2.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.Log4NetAppender" Version="2.21.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.4" />
<PackageReference Include="Seq.Client.Log4Net" Version="3.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="log4net" Version="3.6.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.23.0" />
<PackageReference Include="Microsoft.ApplicationInsights.Log4NetAppender" Version="2.23.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="8.0.0" />
<PackageReference Include="Seq.Client.Log4Net" Version="4.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using log4net.Config;
using log4net;
using log4net;
using log4net.Config;

namespace Log4netAppenders
namespace Log4netAppenders;

public static class Log4netExtensions
{
public static class Log4netExtensions
public static void AddLog4net(this IServiceCollection services)
{
public static void AddLog4net(this IServiceCollection services)
{
XmlConfigurator.Configure(new FileInfo("log4net.config"));
services.AddSingleton(LogManager.GetLogger(typeof(Program)));
}
XmlConfigurator.Configure(new FileInfo("log4net.config"));
services.AddSingleton(LogManager.GetLogger(typeof(Program)));
}
}
}
53 changes: 18 additions & 35 deletions dotnet-logging/Log4netAppenders/Log4netAppenders/Program.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
using Log4netAppenders;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;

namespace Log4netAppenders
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddLog4net();

builder.Services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
{
ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"],
EnableActiveTelemetryConfigurationSetup = true
});
var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();
builder.Services.AddControllers();
builder.Services.AddLog4net();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
var aiConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];
if (!string.IsNullOrWhiteSpace(aiConnectionString))
{
builder.Services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
{
ConnectionString = aiConnectionString,
EnableActiveTelemetryConfigurationSetup = true
});
}

app.UseAuthorization();
var app = builder.Build();

app.MapControllers();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();
}
}
}
app.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"launchBrowser": false,
"applicationUrl": "https://localhost:7076;http://localhost:5264",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
},
"AllowedHosts": "*",
"ApplicationInsights": {
"ConnectionString": "{Provide the Application Insights connection string here}"
"ConnectionString": ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd-HHmm'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="AdoNetAppender" type="MicroKnights.Logging.AdoNetAppender, MicroKnights.Log4NetAdoNetAppender">
<connectionType value="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient,
Version=1.0.0.0,Culture=neutral,PublicKeyToken=23ec7fc2d6eaa4a5" />
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient" />
<connectionString value="{Provide the DB connection string here}" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message])
VALUES (@log_date, @thread, @log_level, @logger, @message)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
Expand Down
14 changes: 7 additions & 7 deletions dotnet-logging/Log4netAppenders/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,15 +10,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="11.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="Moq" Version="4.21.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.1.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading