使用Ocelot并尝试使用接口

时间:2018-06-05 01:01:28

标签: .net-core nlog api-gateway ocelot

我是.net core和Ocelot的新手。我已经为API网关建立了一个带有ocelot的.net核心2.0框架。对于这个服务,我想添加我的自定义Nlog与接口,并使其可以从main.cs注入(ocelot有内置的nlog,但这对我没有好处)。我在下面提到我的代码。问题出在我的LogMessageClass.cs中,当我从构造函数中注入_log时,日志记录失败。

      public class Program
        {
            private static INLogger _logger;
            public static void Main(string[] args)
           {
//setup logger
            var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
logger.Debug("init main"); // this works

 new WebHostBuilder()
                    .UseKestrel()
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .ConfigureAppConfiguration((hostingContext, config) =>
                    {
                        config
                            .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                            .AddJsonFile("appsettings.json", true, true)
                            .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true,
                                true)
                            .AddJsonFile("ocelot.json")
                            .AddEnvironmentVariables();

                        _configuration = config.Build();

                    })
                    .ConfigureServices((builderContext, services) =>
                    {//set up log factory
                        _loggerFactory = new NLoggerFactory();
                        _configuration.GetSection("Logging").Bind(_loggerFactory);
                        services.AddSingleton<INLoggerFactory, NLoggerFactory>();
                        services.AddSingleton<INLogger, NLogger>();

                        //set up logger
                        services.AddSingleton<ILoggerFactory, LoggerFactory>();
                        services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
                        services.AddLogging((builder) => builder.SetMinimumLevel(LogLevel.Trace));


                        services.AddOcelot()
                            var serviceProvider = services.BuildServiceProvider();
                        var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();

                        //configure NLog
                        loggerFactory.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true });
                        NLog.LogManager.LoadConfiguration("nlog.config");
                        _pipelineHelper = serviceProvider.GetService<IPipelineHelper>();
                    })
                    .ConfigureLogging((hostingContext, logging) =>
                    {
                        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                      //  logging.AddConsole();
                      //  logging.AddDebug();
                    })
                   .UseIISIntegration()
                    .Configure(app =>
                    {

                   })
                    .Build()
                    .Run();
           }
        }

在我的班级 - test.cs

 public class Test
    {
private readonly INLogger _nLogger;
 public Test(INLogger nLogger)
        {
            _nLogger = nLogger;
        }
 public void Log()
        {
            _nLogger.Info("Log my message" ); // this fails
}

}

注意: 1.从主方法登录工作 2.从测试类登录失败

我正在使用nlog.config并记录到文本文件。

感谢任何帮助。

0 个答案:

没有答案