gpt4 book ai didi

c# - 在 ASP.NET Core 2.2 中使用 InProcess 托管模型时,Serilog 不会将日志写入文件

转载 作者:太空狗 更新时间:2023-10-29 21:00:39 26 4
gpt4 key购买 nike

如果我使用新引入的InProcess ASP.NET Core 2.2 中的托管模型如下:

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

Serilog不将日志写入文件。但如果我删除 <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>来自 .csproj一切都按预期工作。

我的 Serilog Program 中的配置类如下:

public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information() // Set the minimun log level
.WriteTo.File("Logs\\log-.txt", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7) // this is for logging into file system
.CreateLogger();

try
{
Log.Information("Starting web host");
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}

}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(logging => { logging.ClearProviders(); }) // clearing all other logging providers
.UseSerilog(); // Using serilog
}

请专家给点意见!

最佳答案

正如对您的问题本身的评论中所建议的,当使用 InProcess 托管模型运行时,应用程序的当前目录与 OutOfProcess 托管模型不同。对于 InProcess,此目录是 IIS 本身的位置 - 例如C:\Program Files\IIS Express,这意味着您的日志文件正在写入 C:\Program Files\IIS Express\Logs\log-.txt(假设设置相关权限)。

解决方法详见 this GitHub issue , 它提供了一个辅助类 ( CurrentDirectoryHelpers ) 来设置正确的当前目录。 SetCurrentDirectory 静态方法使用 PInvoke,确定应用程序是否从 IIS 中运行,如果是,则根据完整的应用程序路径设置当前目录。使用这种方法看起来像这样:

public class Program
{
public static void Main(string[] args)
{
CurrentDirectoryHelpers.SetCurrentDirectory();

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information() // Set the minimun log level
.WriteTo.File("Logs\\log-.txt", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7) // this is for logging into file system
.CreateLogger();

...
}
}

为了完整性,这里有 CurrentDirectoryHelpers:

using System;

namespace SampleApp
{
internal class CurrentDirectoryHelpers
{
internal const string AspNetCoreModuleDll = "aspnetcorev2_inprocess.dll";

[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(string lpModuleName);

[System.Runtime.InteropServices.DllImport(AspNetCoreModuleDll)]
private static extern int http_get_application_properties(ref IISConfigurationData iiConfigData);

[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
private struct IISConfigurationData
{
public IntPtr pNativeApplication;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzFullApplicationPath;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzVirtualApplicationPath;
public bool fWindowsAuthEnabled;
public bool fBasicAuthEnabled;
public bool fAnonymousAuthEnable;
}

public static void SetCurrentDirectory()
{
try
{
// Check if physical path was provided by ANCM
var sitePhysicalPath = Environment.GetEnvironmentVariable("ASPNETCORE_IIS_PHYSICAL_PATH");
if (string.IsNullOrEmpty(sitePhysicalPath))
{
// Skip if not running ANCM InProcess
if (GetModuleHandle(AspNetCoreModuleDll) == IntPtr.Zero)
{
return;
}

IISConfigurationData configurationData = default(IISConfigurationData);
if (http_get_application_properties(ref configurationData) != 0)
{
return;
}

sitePhysicalPath = configurationData.pwzFullApplicationPath;
}

Environment.CurrentDirectory = sitePhysicalPath;
}
catch
{
// ignore
}
}
}
}

关于c# - 在 ASP.NET Core 2.2 中使用 InProcess 托管模型时,Serilog 不会将日志写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53846333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com