Skip to content

Commit 18db4ca

Browse files
authored
Merge pull request #6 from Mathavana/develop
Develop - Logger (Serilog)
2 parents 8d2eb9d + c7c7c20 commit 18db4ca

File tree

7 files changed

+38
-8
lines changed

7 files changed

+38
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ Supermarket.Identity/obj/**
66
Supermarket.Persistent/bin/**
77
Supermarket.Persistent/obj/**
88
Supermarket/bin/**
9-
Supermarket/obj/**
9+
Supermarket/obj/**
10+
Supermarket/LogFiles/**

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ We will try to create an API application using asp.net core.
66
- ASP NET Identity, JWT Authentication implemented in a separate DB
77
- Swagger for API Document
88
- API Version
9+
- Logging (Serilog)
910

1011
### Next ToDo List:
1112
- Role Based Authentication/Authorization
1213
- Standard response for both error/success results
13-
- Logging
1414
- Global Error Handling
1515
- Dockerization
1616

Supermarket/Startup.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using Microsoft.AspNetCore.Mvc.ApiExplorer;
66
using Microsoft.Extensions.Configuration;
77
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Logging;
89
using Newtonsoft.Json.Serialization;
10+
using Serilog;
911
using Supermarket.Extensions;
1012
using Supermarket.Resources;
1113

@@ -15,6 +17,8 @@ public class Startup
1517
{
1618
public Startup(IConfiguration configuration)
1719
{
20+
//Initialize Serilog configuration
21+
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger();
1822
Configuration = configuration;
1923
}
2024

@@ -58,8 +62,10 @@ public void ConfigureServices(IServiceCollection services)
5862
}
5963

6064
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
61-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApiVersionDescriptionProvider provider)
65+
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
66+
IApiVersionDescriptionProvider provider, ILoggerFactory loggerFactory)
6267
{
68+
loggerFactory.AddSerilog();
6369
if (env.IsDevelopment())
6470
{
6571
app.UseDeveloperExceptionPage();

Supermarket/Supermarket.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="3.2.0" />
1919
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
2020
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
21+
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
22+
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
23+
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
2124
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
2225
</ItemGroup>
2326

Supermarket/Swagger/SwaggerInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static Info CreateInfoForApiVersion(ApiVersionDescription description, Ty
1616
{
1717
Title = $"{type.Assembly.GetCustomAttribute<AssemblyProductAttribute>().Product} {description.ApiVersion}",
1818
Version = description.ApiVersion.ToString(),
19-
Description = "A simple example ASP.NET Core Web API",
19+
Description = "Supermarket ASP.NET Core Web API",
2020
TermsOfService = "None",
2121
Contact = new Contact { Name = "Mathavan N", Email = "mathavan@gmail.com", Url = "https://github.com/Mathavana" },
2222
License = new License { Name = "Use under LICX", Url = "https://example.com/license" }

Supermarket/V1/Controllers/AboutController.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.Extensions.Logging;
23

34
namespace Supermarket.V1.Controller
45
{
56
[ApiController]
6-
[ApiVersion("1.0")]
7+
[ApiVersion("1.0", Deprecated = true)]
78
[Route("api/v{version:apiVersion}/[controller]")]
89
public class AboutController : ControllerBase
910
{
11+
private readonly ILogger<AboutController> _logger;
12+
13+
public AboutController(ILogger<AboutController> logger)
14+
{
15+
_logger = logger;
16+
}
17+
1018
[HttpGet]
1119
public IActionResult Get()
1220
{
21+
_logger.LogInformation("Still consuming deprecated api method");
22+
1323
return Ok(new { message = "This is about Api Version 1.0" });
1424
}
1525
}

Supermarket/appsettings.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Warning"
2+
"Serilog": {
3+
"MinimumLevel": "Information",
4+
"WriteTo": [
5+
{
6+
"Name": "RollingFile",
7+
"Args": {
8+
"pathFormat": "LogFiles/FileOn_{Date}.txt",
9+
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}"
10+
}
11+
}
12+
],
13+
"Properties": {
14+
"Application": "Supermarket ASP.NET Core Web API"
515
}
616
},
717
"ConnectionStrings": {

0 commit comments

Comments
 (0)