受欢迎的博客标签

Miniblog.Core Project:ASP.NET Core 6.x-W3CLogger

Published

 

github source:

https://github.com/dotnet/aspnetcore/blob/a450cb69b5e4549f5515cdb057a68771f56cefd7/src/Middleware/HttpLogging/src/W3CLogger.cs

(aspnetcore/src/Middleware/HttpLogging/src/W3CLogger.cs)

Step 1:

using Microsoft.AspNetCore.HttpLogging;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddW3CLogging(logging =>
{
    // Log all W3C fields
    logging.LoggingFields = W3CLoggingFields.All;

    ////logging.FileSizeLimit = 5 * 1024 * 1024;
    ////logging.RetainedFileCountLimit = 2;
    ////logging.FileName = "MyLogFile";
    ////logging.LogDirectory = @"C:\logs";
    //logging.FlushInterval = TimeSpan.FromSeconds(2);
});

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

// Configure the HTTP request pipeline.

app.UseAuthorization();

app.MapControllers();

app.Run();

Default LogDirectory and log file:

Debug Configure

Stockso.WebApi\wwwroot\
Stockso.WebApi\logs\w3clog-20211112.0000.txt
#Version: 1.0
#Start-Date: 2021-11-11 16:53:41
#Fields: date time c-ip cs-username s-computername s-ip s-port cs-method cs-uri-stem cs-uri-query sc-status time-taken cs-version cs-host cs(User-Agent) cs(Cookie) cs(Referer)
2021-11-11 16:53:41 127.0.0.1 - SRKSERVER 127.0.0.1 15793 GET /v1/StockFullABK/GetMainFullABK - 200 76.7614 HTTP/1.1 testapi.stockso.com Go-http-client/1.1 - -

Release Configure

Step 1:

app.UseW3CLogging();

app.MapGet("/first-w3c-log", (IWebHostEnvironment webHostEnvironment) =>
{
    return Results.Ok(new { PathToWrite = webHostEnvironment.ContentRootPath });
})
.WithName("GetW3CLog");

output

{"pathToWrite":"/var/www/Miniblog.Core/Miniblog.Core/src/bin/Release/net6.0/publish/"}

Step 2:chmod 777

vultr:~# cd  /var/Miniblog.Core/src/bin/Release/net6.0/publish/logs
# chmod logs 777

output

s# ls -l
total 8
-rw-r--r-- 1 root     root      741 Jun  9 21:11 w3clog-20220609.0000.txt
-rw-r--r-- 1 www-data www-data 1816 Jun 19 23:57 w3clog-20220619.0000.txt

 

http://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/w3c-logger/index.md

https://github.com/dotnet/aspnetcore/search?q=app.UseW3CLogging

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/w3c-logger/?view=aspnetcore-6.0