当托管在Azure中时,AspNet Core Logging可以运行,但不能在ServiceStack服务中运行

时间:2019-01-16 11:40:45

标签: asp.net-core servicestack asp.net-core-2.2

我有一个简单的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

会发生什么?

如果在“启动”中添加一些日志,则会得到可爱的日志记录。但是,当托管在Azure中时,ServiceStack服务中的日志记录不会出现。在本地运行时,确实会记录日志。

因此NetCore可以正常登录,但是Service类中的任何内容都不可以!

为什么不对此进行记录?

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()
            };
        }

2 个答案:

答案 0 :(得分:1)

最后,这是一个愚蠢的路由问题,与Controller中的方法匹配,而不是陷入接口模型上定义的ServiceStack路由中。测试时我会闲逛的一种方法。

答案 1 :(得分:0)

尝试在ServiceStack初始化之前配置LogFactory,例如:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    LogManager.LogFactory = new Log4NetFactory(configureLog4Net: true);

    app.UseServiceStack(new AppHost
    {
        AppSettings = new NetCoreAppSettings(Configuration)
    });
}

或在服务中使用实例记录器:

public readonly ILog log = LogManager.GetLogger(typeof(ServiceBase));