托管在Azure中时,AspNet核心日志记录可以工作,但不能在ServiceStack服务内部工作


我有一个简单的ServiceStack服务,并添加了一些日志。

log.Info("In Vehicle service request");
if (log.IsDebugEnabled)
    log.Debug("Debugging Vehicle service request");

log在基类中定义如下;

public abstract class ServiceBase : Service
{
    public static readonly ILog log = LogManager.GetLogger(typeof(ServiceBase));
}

web主机被配置为添加各种日志提供程序,包括log4net (注意:我已经尝试过其他日志提供程序=相同的问题)。

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((context, config) =>
                {
                    config.SetBasePath(Directory.GetCurrentDirectory());
                    config
                        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                        .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
                        .AddEnvironmentVariables();     //lets Azure portal override settings
                    context.Configuration = config.Build();
                })
                .ConfigureLogging((hostingContext, logging) =>
                {
                    logging.ClearProviders();
                    logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                    logging.AddConsole();
                    logging.AddDebug();
                    logging.AddEventSourceLogger();
                    logging.AddAzureWebAppDiagnostics();
                    // The ILoggingBuilder minimum level determines the
                    // the lowest possible level for logging. The log4net
                    // level then sets the level that we actually log at.
                    logging.AddLog4Net();
                    //logging.SetMinimumLevel(LogLevel.Debug);
                })
                .UseAzureAppServices()
                .UseStartup<Startup>();

ServiceStack AppHost提前设置LogFactory:

public override void Configure(Container container)
{
    //Also runs log4net.Config.XmlConfigurator.Configure()
    LogManager.LogFactory = new Log4NetFactory(configureLog4Net: true);
..etc

会发生什么?

如果我在我的StartUp中添加一些,我会得到很好的日志记录。然而,当托管在Azure中时,ServiceStack服务中的日志记录不会出现。当我在本地运行时,我确实得到了日志。

所以NetCore可以正常记录日志,但是服务类中的任何内容都不是!

为什么没有日志记录呢?

public async Task<GetMyDataResponse> Any(GetMyData request)
        {
            log.Info("In service request");
            if (log.IsDebugEnabled)
                log.Debug("Debugging service request");

            //Some request validation logic could/should go here.
            return new GetMyDataResponse
            {
                Results = await _myDataRepo.FetchAsync()
            };
        }

转载请注明出处:http://www.gxainuo.com/article/20230526/2462869.html