[英]ASP.NET Core Web API - How to pass specified fields as payload to third party API
[英]How to log to a file in asp.net core web app - wtihout any third party tools
我目前正在运行一个应用程序,我希望将日志放入文件中,以便 Datadog 能够获取它们。
我目前只使用 sourcegenerator 进行日志记录,但是如何将这些日志记录到文件中?
我尝试将 web.config 更改为并将其部署到 iis,但似乎没有任何内容记录到文件中,然后我手动创建了日志文件夹,但里面似乎仍然没有任何内容。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- To customize the asp.net core module uncomment and edit the following section.
For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->
<system.webServer>
<handlers>
<remove name="aspNetCore"/>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</configuration>
那么如何将我的日志记录到文件中呢?
我目前如何登录
public partial class RequestResponseLoggerMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
public RequestResponseLoggerMiddleware(RequestDelegate next,
ILoggerFactory loggerFactory)
{
_next = next;
_logger = loggerFactory
.CreateLogger<RequestResponseLoggerMiddleware>();
}
[LoggerMessage(0, LogLevel.Information, "{requestUrl} proxied to {proxiedUrl}")]
partial void LogRequest(string requestUrl, string proxiedUrl);
public async Task Invoke(HttpContext context)
{
//code dealing with the request
string requestUrl = context.Request.GetDisplayUrl();
string path = context.Request.Path;
await _next(context);
var proxyFeature = context.GetReverseProxyFeature();
Yarp.ReverseProxy.Model.DestinationState? destination = proxyFeature.ProxiedDestination;
if (destination != null)
{
string proxiedUrl = destination.Model.Config.Address + path;
//code dealing with the response
LogRequest(requestUrl, proxiedUrl);
}
else
{
LogRequest(requestUrl, string.Empty);
}
}
}
程序.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
var app = builder.Build();
app.MapReverseProxy(proxyPipeline =>
{
proxyPipeline.UseRequestResponseLogging();
});
app.UseHttpsRedirection();
app.Run();
应用设置.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"force": {
"ClusterId": "old-site",
"Match": {
"Path": "{**catch-all}"
}
},
"azure": {
"ClusterId": "new-site",
"Match": {
"Path": "yarpb"
}
}
},
"Clusters": {
"old-site": {
"Destinations": {
"force": {
"Address": "https://example.com/"
}
}
},
"new-site": {
"Destinations": {
"yarpb": {
"Address": "https://localhost:61000/"
}
}
}
}
}
}
问题http://github.com/aspnet/Logging/issues/441已关闭,MS 官方建议使用 3rd 方文件记录器。 请参阅以下答案: 如何在 .Net Core 中不使用第三方记录器的情况下登录文件?
有点不同的方法,但如果您使用 Datadog,请考虑仅写入 Windows 事件查看器
public void WriteToEventLog(string sLog, string sSource, string message, EventLogEntryType level) {
if (!EventLog.SourceExists(sSource)) EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, message, level);
}
然后使用 Datadog:
- type: windows_event
channel_path: System
source: System
service: eventlog
log_processing_rules:
- type: exclude_at_match
name: exclude_information_event
pattern: ^.*[Ll]evel.*Information.*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.