Support download full log.

This commit is contained in:
Haiping Chen 2024-01-10 22:16:13 -06:00
parent eaf112fc26
commit 66d45acfc3
4 changed files with 42 additions and 3 deletions

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

View file

@ -41,7 +41,7 @@ public static class BotSharpOpenApiExtensions
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["Jwt:Key"])),
ValidateIssuer = enableValidation,
ValidateAudience = enableValidation,
ValidateLifetime = enableValidation,
ValidateLifetime = enableValidation && !env.IsDevelopment(),
ValidateIssuerSigningKey = enableValidation
};

View file

@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Hosting;
using SharpCompress.Compressors.Xz;
using System;
using System.IO;
using System.Net.Mime;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]
public class LoggerController : ControllerBase
{
private readonly IServiceProvider _services;
public LoggerController(IServiceProvider services)
{
_services = services;
}
[HttpGet("/logger/full-log")]
public async Task<IActionResult> GetFullLog()
{
var env = _services.GetRequiredService<IWebHostEnvironment>();
var logFile = Path.Combine(env.ContentRootPath, "logs", $"log-{DateTime.Now:yyyyMMdd}.txt");
if (System.IO.File.Exists(logFile))
{
using Stream stream = System.IO.File.Open(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
return File(bytes, "application/octet-stream", Path.GetFileName(logFile));
}
else
{
return NotFound();
}
}
}

View file

@ -14,7 +14,9 @@ Log.Logger = new LoggerConfiguration()
.MinimumLevel.Warning()
#endif
.WriteTo.Console()
.WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day)
.WriteTo.File("logs/log-.txt",
shared: true,
rollingInterval: RollingInterval.Day)
.CreateLogger();
builder.Host.UseSerilog();