亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

C#
慕雪6442864 2022-06-18 17:16:48
如果我使用新引入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 }請高手給點意見!
查看完整描述

2 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

正如對您問題本身的評論中所建議的那樣,在使用InProcess托管模型運行時,應用程序的當前目錄與OutOfProcess托管模型不同。對于InProcess,此目錄是 IIS 本身的位置 - 例如C:\Program Files\IIS Express,這意味著正在寫入您的日志文件C:\Program Files\IIS Express\Logs\log-.txt(假設已設置相關權限)。


此 GitHub 問題中詳細介紹了解決此問題的方法,它提供了一個幫助類 ( CurrentDirectoryHelpers) 用于設置正確的當前目錄。靜態方法使用SetCurrentDirectoryPInvoke,確定應用程序是否在 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

            }

        }

    }

}


查看完整回答
反對 回復 2022-06-18
?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

嘗試升級 .Net Core 版本。此問題似乎已在 2.2.3 中修復。



查看完整回答
反對 回復 2022-06-18
  • 2 回答
  • 0 關注
  • 283 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號