-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add structured logging and initial project setup
- Added Visual Studio solution file `StructuredLoggingNET9.sln`. - Created `ApplicationService.cs` with Serilog configuration. - Added `appsettings.Development.json` for development logging levels. - Added `appsettings.json` for application logging levels and settings. - Implemented Serilog in `Program.cs` for structured logging. - Created `StructuredLoggingNET9.csproj` targeting .NET 9.0 with necessary package references. - Added `StructuredLoggingNET9.http` with sample HTTP requests. - Created `WeatherForecast` class in `WeatherForecast.cs`. - Implemented `WeatherForecastController` with logging in `WeatherForecastController.cs`. - Added `launchSettings.json` for development environment configuration.
- Loading branch information
1 parent
8db0de1
commit 5bb8e70
Showing
11 changed files
with
371 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.12.35506.116 d17.12 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StructuredLoggingNET9", "StructuredLoggingNET9\StructuredLoggingNET9.csproj", "{1FCAD008-CF67-4760-B278-CA3F4E34D428}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{1FCAD008-CF67-4760-B278-CA3F4E34D428}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{1FCAD008-CF67-4760-B278-CA3F4E34D428}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{1FCAD008-CF67-4760-B278-CA3F4E34D428}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{1FCAD008-CF67-4760-B278-CA3F4E34D428}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |
19 changes: 19 additions & 0 deletions
19
StructuredLoggingNET9/StructuredLoggingNET9/ApplicationService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Serilog; | ||
using Serilog.Formatting.Json; | ||
|
||
namespace StructuredLoggingNET9 | ||
{ | ||
public static class ApplicationService | ||
{ | ||
public static void ConfigureLogging(this IHostBuilder host) | ||
{ | ||
host.UseSerilog((ctx, lc) => | ||
{ | ||
lc.WriteTo.Console(); | ||
lc.WriteTo.Seq("http://localhost:5341"); | ||
lc.WriteTo.File(new JsonFormatter(), "log.txt"); | ||
|
||
}); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
StructuredLoggingNET9/StructuredLoggingNET9/Controllers/WeatherForecastController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace StructuredLoggingNET9.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
private readonly ILogger<WeatherForecastController> _logger; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[HttpGet(Name = "GetWeatherForecast")] | ||
public IEnumerable<WeatherForecast> Get() | ||
{ | ||
_logger.LogInformation("Getting weather forecast"); | ||
var forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
}) | ||
.ToArray(); | ||
_logger.LogInformation("Generated weather forecasts: {Forecasts}", forecasts); | ||
|
||
_logger.LogWarning("This is a warning message"); | ||
|
||
return forecasts; | ||
} | ||
|
||
[HttpGet("date/{date}")] | ||
public ActionResult<WeatherForecast> GetByDate(DateOnly date) | ||
{ | ||
_logger.LogInformation("Entering GetByDate method with date: {Date}", date); | ||
|
||
var forecast = new WeatherForecast | ||
{ | ||
Date = date, | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
}; | ||
|
||
_logger.LogInformation("Generated weather forecast: {Forecast}", forecast); | ||
|
||
_logger.LogInformation("Exiting GetByDate method"); | ||
|
||
return Ok(forecast); | ||
} | ||
|
||
[HttpPost("add")] | ||
public IActionResult AddForecast([FromBody] WeatherForecast forecast) | ||
{ | ||
_logger.LogInformation("Entering AddForecast method with forecast: {Forecast}", forecast); | ||
|
||
// Here you would typically add the forecast to a database or in-memory collection | ||
// For this example, we'll just log the forecast and return it | ||
|
||
_logger.LogInformation("Added weather forecast: {Forecast}", forecast); | ||
|
||
_logger.LogInformation("Exiting AddForecast method"); | ||
|
||
return CreatedAtAction(nameof(GetByDate), new { date = forecast.Date }, forecast); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Serilog; | ||
using StructuredLoggingNET9; | ||
|
||
Log.Logger = new LoggerConfiguration() | ||
.WriteTo.Console() | ||
.CreateLogger(); | ||
|
||
try | ||
{ | ||
|
||
Log.Information("Starting up the application"); | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
|
||
|
||
builder.Host.ConfigureLogging(); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi | ||
builder.Services.AddOpenApi(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.MapOpenApi(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); | ||
|
||
|
||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Fatal(ex, "The application failed to start correctly"); | ||
} | ||
finally | ||
{ | ||
Log.CloseAndFlush(); | ||
} |
23 changes: 23 additions & 0 deletions
23
StructuredLoggingNET9/StructuredLoggingNET9/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": false, | ||
"applicationUrl": "http://localhost:5005", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": false, | ||
"applicationUrl": "https://localhost:7202;http://localhost:5005", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
StructuredLoggingNET9/StructuredLoggingNET9/StructuredLoggingNET9.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" /> | ||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" /> | ||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" /> | ||
<PackageReference Include="Serilog.Sinks.Seq" Version="9.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
21 changes: 21 additions & 0 deletions
21
StructuredLoggingNET9/StructuredLoggingNET9/StructuredLoggingNET9.http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@StructuredLoggingDotnet9_HostAddress = http://localhost:5005 | ||
|
||
### Get Weather Forecasts | ||
GET {{StructuredLoggingDotnet9_HostAddress}}/weatherforecast/ | ||
Accept: application/json | ||
|
||
|
||
### Get Weather Forecast by Date | ||
GET {{StructuredLoggingDotnet9_HostAddress}}/weatherforecast/date/2023-10-10 | ||
Accept: application/json | ||
|
||
### Add New Weather Forecast | ||
POST {{StructuredLoggingDotnet9_HostAddress}}/weatherforecast/add | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
{ | ||
"date": "2023-10-10", | ||
"temperatureC": 25, | ||
"summary": "Warm" | ||
} |
13 changes: 13 additions & 0 deletions
13
StructuredLoggingNET9/StructuredLoggingNET9/WeatherForecast.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace StructuredLoggingNET9 | ||
{ | ||
public class WeatherForecast | ||
{ | ||
public DateOnly Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
|
||
public string? Summary { get; set; } | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
StructuredLoggingNET9/StructuredLoggingNET9/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
Oops, something went wrong.