猿问

在 ASP.NET Core 2.2 中使用 InProcess 托管模型时

如果我使用新引入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 

}

请高手给点意见!


慕雪6442864
浏览 216回答 2
2回答

繁花如伊

正如对您问题本身的评论中所建议的那样,在使用InProcess托管模型运行时,应用程序的当前目录与OutOfProcess托管模型不同。对于InProcess,此目录是 IIS 本身的位置 - 例如C:\Program Files\IIS Express,这意味着正在写入您的日志文件C:\Program Files\IIS Express\Logs\log-.txt(假设已设置相关权限)。此 GitHub 问题中详细介绍了解决此问题的方法,它提供了一个帮助类 ( CurrentDirectoryHelpers) 用于设置正确的当前目录。静态方法使用SetCurrentDirectoryPInvoke,确定应用程序是否在 IIS 中运行,如果是,则根据完整的应用程序路径设置当前目录。使用这种方法看起来像这样:public class Program{&nbsp; &nbsp; public static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; CurrentDirectoryHelpers.SetCurrentDirectory();&nbsp; &nbsp; &nbsp; &nbsp; Log.Logger = new LoggerConfiguration()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .MinimumLevel.Information() // Set the minimun log level&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .WriteTo.File("Logs\\log-.txt", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7) // this is for logging into file system&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .CreateLogger();&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }}这是CurrentDirectoryHelpers为了完整性:using System;namespace SampleApp{&nbsp; &nbsp; internal class CurrentDirectoryHelpers&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; internal const string AspNetCoreModuleDll = "aspnetcorev2_inprocess.dll";&nbsp; &nbsp; &nbsp; &nbsp; [System.Runtime.InteropServices.DllImport("kernel32.dll")]&nbsp; &nbsp; &nbsp; &nbsp; private static extern IntPtr GetModuleHandle(string lpModuleName);&nbsp; &nbsp; &nbsp; &nbsp; [System.Runtime.InteropServices.DllImport(AspNetCoreModuleDll)]&nbsp; &nbsp; &nbsp; &nbsp; private static extern int http_get_application_properties(ref IISConfigurationData iiConfigData);&nbsp; &nbsp; &nbsp; &nbsp; [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]&nbsp; &nbsp; &nbsp; &nbsp; private struct IISConfigurationData&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public IntPtr pNativeApplication;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string pwzFullApplicationPath;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string pwzVirtualApplicationPath;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public bool fWindowsAuthEnabled;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public bool fBasicAuthEnabled;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public bool fAnonymousAuthEnable;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public static void SetCurrentDirectory()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Check if physical path was provided by ANCM&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sitePhysicalPath = Environment.GetEnvironmentVariable("ASPNETCORE_IIS_PHYSICAL_PATH");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (string.IsNullOrEmpty(sitePhysicalPath))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Skip if not running ANCM InProcess&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (GetModuleHandle(AspNetCoreModuleDll) == IntPtr.Zero)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IISConfigurationData configurationData = default(IISConfigurationData);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (http_get_application_properties(ref configurationData) != 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sitePhysicalPath = configurationData.pwzFullApplicationPath;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Environment.CurrentDirectory = sitePhysicalPath;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ignore&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

隔江千里

尝试升级 .Net Core 版本。此问题似乎已在 2.2.3 中修复。
随时随地看视频慕课网APP
我要回答