🔊 logs added for customer
continuous-integration/drone/push Build was killed Details

pull/42/head
Ismail TAHA JANAN 2 years ago
parent 0240a45e99
commit 5ee1a19119

BIN
.DS_Store vendored

Binary file not shown.

@ -9,6 +9,8 @@ using cat_cafe.Entities;
using cat_cafe.Repositories;
using cat_cafe.Dto;
using AutoMapper;
using Serilog;
using Newtonsoft.Json;
namespace cat_cafe.Controllers
{
@ -18,18 +20,27 @@ namespace cat_cafe.Controllers
{
private readonly CustomerContext _context;
private readonly IMapper _mapper;
private readonly ILogger<CustomersController> _logger;
public CustomersController(CustomerContext context,IMapper mapper)
public CustomersController(CustomerContext context,IMapper mapper,ILogger<CustomersController> logger)
{
_context = context;
_mapper = mapper;
_logger = logger;
}
// GET: api/Customers
[HttpGet]
public async Task<ActionResult<IEnumerable<CustomerDto>>> GetCustomers()
{
Log.Information(this.Request.Method + " => get All customers");
var customers = await _context.Customers.ToListAsync();
Log.Information(this.Request.Method + " => "
+ this.Response.StatusCode.ToString() + " "
+ customers.GetType().ToString() + " length["
+ customers.Count + "]");
return Ok(_mapper.Map<List<CustomerDto>>(customers));
}
@ -37,13 +48,19 @@ namespace cat_cafe.Controllers
[HttpGet("{id}")]
public async Task<ActionResult<CustomerDto>> GetCustomer(long id)
{
Log.Information(this.Request.Method + " => get by ID {@id}",id);
var customer = await _context.Customers.FindAsync(id);
if (customer == null)
{
Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString());
return NotFound();
}
Log.Information(this.Request.Method + " => "
+ this.Response.StatusCode.ToString() + " "
+ customer.GetType().ToString() + " "
+ JsonConvert.SerializeObject(customer).ToString());
return _mapper.Map<CustomerDto>(customer);
}
@ -52,8 +69,10 @@ namespace cat_cafe.Controllers
[HttpPut("{id}")]
public async Task<IActionResult> PutCustomer(long id, CustomerDto customerDto)
{
Log.Information(this.Request.Method + " => put by ID {@id}", id);
if (id != customerDto.Id)
{
Log.Information(this.Request.Method + " => " + BadRequest().StatusCode.ToString()+" IDs not matching");
return BadRequest();
}
@ -65,19 +84,24 @@ namespace cat_cafe.Controllers
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
catch (DbUpdateConcurrencyException e)
{
if (!CustomerExists(id))
{
Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString());
return NotFound();
}
else
{
Log.Error(this.Request.Method + " => " + e.Message);
throw;
}
}
return NoContent();
Log.Information(this.Request.Method + " => "
+ this.Response.StatusCode.ToString() + " "
+ customer.GetType().ToString() + " "
+ JsonConvert.SerializeObject(customer).ToString());
return Ok();
}
// POST: api/Customers
@ -85,10 +109,17 @@ namespace cat_cafe.Controllers
[HttpPost]
public async Task<ActionResult<Customer>> PostCustomer(CustomerDto customerDto)
{
Log.Information(this.Request.Method + " => post customer");
Customer customer = _mapper.Map<Customer>(customerDto);
_context.Customers.Add(customer);
await _context.SaveChangesAsync();
Log.Information(this.Request.Method + " => "
+ 201 + " "
+ customer.GetType().ToString() + " "
+ JsonConvert.SerializeObject(customer).ToString());
return CreatedAtAction("GetCustomer", new { id = customer.Id }, _mapper.Map<Customer>( customer));
}
@ -96,16 +127,24 @@ namespace cat_cafe.Controllers
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteCustomer(long id)
{
Log.Information(this.Request.Method + " => delete by ID {@id}", id);
var customer = await _context.Customers.FindAsync(id);
if (customer == null)
{
Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString());
return NotFound();
}
_context.Customers.Remove(customer);
await _context.SaveChangesAsync();
return NoContent();
Log.Information(this.Request.Method + " => "
+ this.Response.StatusCode.ToString() + " "
+ customer.GetType().ToString() + " "
+ JsonConvert.SerializeObject(customer).ToString());
return Ok();
}
private bool CustomerExists(long id)

@ -1,8 +1,17 @@
using Microsoft.EntityFrameworkCore;
using cat_cafe.Repositories;
using Serilog;
using Serilog.Sinks.File;
var builder = WebApplication.CreateBuilder(args);
/*
ILoggerFactory loggerFactory=new LoggerFactory();
loggerFactory.AddFile($@"{Directory.GetCurrentDirectory}\Logs\logs.txt");
*/
Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.File("log.txt").CreateLogger();
// Add services to the container.
builder.Services.AddControllers();
@ -21,6 +30,12 @@ builder.Services.AddControllersWithViews();
var app = builder.Build();
/*var loggerFactory = app.Services.GetService<ILoggerFactory>();
loggerFactory.AddFile(builder.Configuration["Logging:LogFilePath"].ToString());
app.Services.GetService<ILoggerFactory>().AddFile(builder.Configuration["Logging:LogFilePath"].ToString());*/
app.UseHttpLogging();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
@ -34,4 +49,5 @@ app.UseAuthorization();
app.MapControllers();
Log.Information("program start");
app.Run();

@ -1,9 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Default": "Information"
//"Microsoft.AspNetCore": "Warning"
}
},
"LogFilePath": "Logs\\log-{Date}.txt",
"AllowedHosts": "*"
}

@ -17,9 +17,15 @@
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<None Remove="AutoMapper.Extensions.Microsoft.DependencyInjection" />
<None Remove="Serilog.Extensions.Logging.File" />
<None Remove="Serilog" />
<None Remove="Serilog.Sinks.File" />
</ItemGroup>
</Project>

@ -0,0 +1,58 @@
2023-01-18 08:31:45.368 +01:00 [INF] hello i m here
2023-01-18 09:01:20.438 +01:00 [INF] GET
2023-01-18 09:04:06.569 +01:00 [INF] GET => cat_cafe.Repositories.CustomerContext
2023-01-18 09:06:53.085 +01:00 [INF] GET => get All customers
2023-01-18 09:06:53.206 +01:00 [INF] GET => System.Collections.Generic.List`1[cat_cafe.Entities.Customer]
2023-01-18 09:06:54.335 +01:00 [INF] GET => get All customers
2023-01-18 09:06:54.345 +01:00 [INF] GET => System.Collections.Generic.List`1[cat_cafe.Entities.Customer]
2023-01-18 09:09:10.113 +01:00 [INF] GET => get All customers
2023-01-18 09:09:10.234 +01:00 [INF] GET => Microsoft.AspNetCore.Mvc.OkResult
2023-01-18 09:09:11.257 +01:00 [INF] GET => get All customers
2023-01-18 09:09:11.267 +01:00 [INF] GET => Microsoft.AspNetCore.Mvc.OkResult
2023-01-18 09:09:21.627 +01:00 [INF] GET => get All customers
2023-01-18 09:09:21.659 +01:00 [INF] GET => Microsoft.AspNetCore.Mvc.OkResult
2023-01-18 09:09:22.556 +01:00 [INF] GET => get All customers
2023-01-18 09:09:22.556 +01:00 [INF] GET => Microsoft.AspNetCore.Mvc.OkResult
2023-01-18 09:13:26.188 +01:00 [INF] GET => get All customers
2023-01-18 09:13:26.308 +01:00 [INF] GET => 200
2023-01-18 09:13:27.367 +01:00 [INF] GET => get All customers
2023-01-18 09:13:27.377 +01:00 [INF] GET => 200
2023-01-18 09:17:33.374 +01:00 [INF] GET => get by ID
2023-01-18 09:17:33.521 +01:00 [INF] GET => 200
2023-01-18 09:27:39.325 +01:00 [INF] GET => get by ID
2023-01-18 09:27:39.471 +01:00 [INF] GET => Microsoft.AspNetCore.Mvc.NotFoundResult
2023-01-18 09:27:40.257 +01:00 [INF] GET => get by ID
2023-01-18 09:27:40.285 +01:00 [INF] GET => Microsoft.AspNetCore.Mvc.NotFoundResult
2023-01-18 09:28:36.156 +01:00 [INF] GET => get by ID
2023-01-18 09:28:36.299 +01:00 [INF] GET => 404
2023-01-18 09:32:05.645 +01:00 [INF] GET => get by ID 98
2023-01-18 09:32:05.788 +01:00 [INF] GET => 404
2023-01-18 09:36:20.083 +01:00 [INF] GET => get All customers
2023-01-18 09:36:20.203 +01:00 [INF] GET => 200System.Collections.Generic.List`1[cat_cafe.Entities.Customer]0
2023-01-18 09:36:21.191 +01:00 [INF] GET => get All customers
2023-01-18 09:36:21.200 +01:00 [INF] GET => 200System.Collections.Generic.List`1[cat_cafe.Entities.Customer]0
2023-01-18 09:37:41.477 +01:00 [INF] GET => get All customers
2023-01-18 09:37:41.599 +01:00 [INF] GET => 200System.Collections.Generic.List`1[cat_cafe.Entities.Customer]0
2023-01-18 09:48:40.093 +01:00 [INF] GET => get All customers
2023-01-18 09:48:40.216 +01:00 [INF] GET => 200List`10
2023-01-18 09:49:47.745 +01:00 [INF] GET => get All customers
2023-01-18 09:50:13.854 +01:00 [INF] GET => get All customers
2023-01-18 10:12:48.174 +01:00 [INF] GET => get All customers
2023-01-18 10:12:48.299 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] 0
2023-01-18 13:40:12.116 +01:00 [INF] GET => get All customers
2023-01-18 13:40:12.243 +01:00 [INF] GET => 200 length[System.Collections.Generic.List`1[cat_cafe.Entities.Customer]] 0
2023-01-18 13:49:39.432 +01:00 [INF] GET => get All customers
2023-01-18 13:49:39.556 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[0]
2023-01-18 13:49:40.662 +01:00 [INF] GET => get All customers
2023-01-18 13:49:40.671 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[0]
2023-01-18 13:50:02.846 +01:00 [INF] GET => get All customers
2023-01-18 13:50:02.858 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[3]
2023-01-18 13:50:16.312 +01:00 [INF] GET => get by ID 1
2023-01-18 13:50:16.372 +01:00 [INF] GET => 200 cat_cafe.Entities.Customer cat_cafe.Entities.Customer
2023-01-18 14:02:49.706 +01:00 [INF] GET => get by ID 1
2023-01-18 14:02:49.853 +01:00 [INF] GET => 200 cat_cafe.Entities.Customer {"Id":1,"FullName":"string","Age":0}
2023-01-18 14:02:50.942 +01:00 [INF] GET => get by ID 1
2023-01-18 14:02:50.955 +01:00 [INF] GET => 200 cat_cafe.Entities.Customer {"Id":1,"FullName":"string","Age":0}
2023-01-18 14:40:46.920 +01:00 [INF] program start
2023-01-18 14:41:47.647 +01:00 [INF] program startMicrosoft.AspNetCore.Server.Kestrel.Core.Internal.ServerAddressesCollection+PublicServerAddressesCollection
2023-01-18 14:46:02.825 +01:00 [INF] program start
Loading…
Cancel
Save