static DependencyInjection() { ServiceCollection = new ServiceCollection(); // Setup config Configure <Configuration.AzureConfiguration>(Configuration.AzureConfiguration.Section); Configure <Configuration.FirebaseConfiguration>(Configuration.FirebaseConfiguration.Section); Configure <Configuration.MulticastConfiguration>(Configuration.MulticastConfiguration.Section); Configure <Configuration.TesterConfiguration>(Configuration.TesterConfiguration.Section); // Configuration var configuration = GetConfiguration(); ServiceCollection.AddSingleton(configuration); ServiceCollection.AddTransient <IRoundtripTester, RoundtripTester>(); ServiceCollection.AddTransient <MulticastWriteTester, MulticastWriteTester>(); ServiceCollection.AddTransient <MulticastListenTester, MulticastListenTester>(); ServiceCollection.AddTransient <IAzureFunctionPublisher, AzureFunctionPublisher>(); ServiceCollection.AddTransient <IMulticastClient, MulticastClient>(); ServiceCollection.AddTransient <IFirebaseChannel, FirebaseChannel>(); ServiceCollection.AddTransient <IPingDiagnosticContainer, PingDiagnosticContainer>(); ServiceCollection.AddTransient <IPingDiagnosticMessageTransformer, PingDiagnosticMessageTransformer>(); // Logging var nlogConfiguration = new NLogLoggingConfiguration(configuration.GetSection("NLog")); ServiceCollection.AddLogging(configure => configure.AddNLog(nlogConfiguration)); ServiceCollection.Configure <LoggerFilterOptions>(options => options.MinLevel = LogLevel.Trace); }
public static NLogLoggingConfiguration UseMongoTarget(this NLogLoggingConfiguration nlogConf, IConfiguration configuration) { var targetOption = configuration.GetSection(nameof(NLogNoSqlTargetOptions)).Get <NLogNoSqlTargetOptions>(); var mongoOption = configuration.GetSection(nameof(LogStorageOptions)).Get <LogStorageOptions>(); if (targetOption == null || mongoOption == null) { throw new NullReferenceException($"Cannot found {nameof(NLogNoSqlTargetOptions)} or {nameof(LogStorageOptions)} from configuration file!"); } var mongoTarget = new MongoTarget() { Name = targetOption.Name, CollectionName = targetOption.CollectionName, ConnectionString = configuration["MongoDBConnection"], DatabaseName = mongoOption.DatabaseName, IncludeDefaults = true }; nlogConf.AddTarget(mongoTarget); foreach (var r in targetOption.Rules) { var minlevel = LogLevel.FromString(r.MinLevel); var maxLevel = LogLevel.FromString(r.MaxLevel); nlogConf.AddRule(minlevel, maxLevel, mongoTarget, r.Logger); } return(nlogConf); }
private void ConfigureServices(IServiceCollection services) { // viewModels services.AddTransient <AboutWindowViewModel>(); services.AddTransient <MainWindowViewModel>(); // services services.AddTransient <IMyCalculator, MyCalculator>(); services.AddSingleton <ViewManager>(); // wanna have only one manager // read external json configurations var config = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); // strongly-typed setting class services.Configure <AppSettings>(config.GetSection(nameof(AppSettings))); // setting up logging services.AddLogging(builder => { var conf = new NLogLoggingConfiguration(config.GetSection(nameof(NLog))); builder.AddNLog(conf); }); }
private static NLogLoggingConfiguration CreateNLogLoggingConfigurationWithNLogSection(IDictionary <string, string> memoryConfig) { var configuration = new ConfigurationBuilder().AddInMemoryCollection(memoryConfig).Build(); var logFactory = new LogFactory(); var logConfig = new NLogLoggingConfiguration(configuration.GetSection("NLog"), logFactory); return(logConfig); }
private static Logger GetLogger() { IConfigurationSection configSection = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .Build() .GetSection("NLog"); LoggingConfiguration loggingConfig = new NLogLoggingConfiguration(configSection); Logger logger = NLogBuilder.ConfigureNLog(loggingConfig).GetCurrentClassLogger(); return(logger); }
internal static void ConfigureNLogOnce( IConfiguration configuration) { if (!NlogConfigurationLock.TryAcquire()) { return; } var nLogConfig = new NLogLoggingConfiguration( configuration.GetSection("NLog")); NLogBuilder.ConfigureNLog(nLogConfig); }
private static IHostBuilder CreateHostBuilder(String[] args, IConfiguration config, ILogger logger) => Host.CreateDefaultBuilder(args) .ConfigureServices((_, services) => services.AddHostedService(_ => BuildService(config, logger))) .ConfigureLogging((context, builder) => { // configure Logging with NLog builder.ClearProviders(); var section = context.Configuration.GetSection("NLog"); var cfg = new NLogLoggingConfiguration(section); builder.AddNLog(cfg); }) .UseWindowsService();
public static ILoggingBuilder AddMetricsLogging(this ILoggingBuilder builder, IConfiguration configuration) { ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; builder.ClearProviders(); var logConfig = new NLogLoggingConfiguration(configuration.GetSection("NLog")); LogManager.Configuration = logConfig; builder.AddProvider(new NLogLoggerProvider(NLogAspNetCoreOptions.Default, new LogFactory(logConfig))); builder.SetMinimumLevel(LogLevel.Trace); return(builder); }
private static ServiceProvider GetServiceProvider() { IServiceCollection serviceCollection = new ServiceCollection(); AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(RegionEndpoint.USEast1); // Logging serviceCollection.AddLogging(loggingBuilder => { loggingBuilder.ClearProviders(); loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); loggingBuilder.AddNLog(); }); // Business Logic serviceCollection.AddSingleton <IDynamoDBContext>(new DynamoDBContext(dynamoDBClient, new DynamoDBContextConfig() { ConsistentRead = true })) .AddSingleton <IAmazonDynamoDB>(dynamoDBClient) .AddTransient <IRequestBusinessLogic, RequestBusinessLogic>() .AddTransient <IRequestMapper, RequestMapper>() .AddTransient <IStarWarsPunRepository, StarWarsPunRepository>(); // SessionEndedRequest serviceCollection.AddTransient <IRequestRouter, SessionEndedRequestRouter>() .AddTransient <ISessionEndedRequestHandler, DefaultSessionEndedRequest>(); // LaunchRequest serviceCollection.AddTransient <IRequestRouter, LaunchRequestRouter>() .AddTransient <ILaunchRequestHandler, DefaultLaunchRequest>(); // IntentRequest serviceCollection.AddTransient <IRequestRouter, IntentRequestRouter>() .AddTransient <IIntentRequestHandler, GetNewPun>() .AddTransient <IIntentRequestHandler, Help>() .AddTransient <IIntentRequestHandler, Fallback>() .AddTransient <IIntentRequestHandler, Stop>() .AddTransient <IIntentRequestHandler, Cancel>() .AddTransient <IIntentRequestHandler, NavigateHome>(); // Set logging configuration LoggingConfiguration nlogConfig = new NLogLoggingConfiguration(Configuration.File.GetSection("NLog")); LogManager.Configuration = nlogConfig; return(serviceCollection.BuildServiceProvider());; }
public void ReloadLogFactoryConfigurationKeepVariables() { var memoryConfig = CreateMemoryConfigConsoleTargetAndRule(); memoryConfig["NLog:Targets:file:type"] = "File"; memoryConfig["NLog:Targets:file:fileName"] = "${var_filename}"; memoryConfig["NLog:autoreload"] = "true"; memoryConfig["NLog:variables:var_filename"] = "hello.txt"; var configuration = new ConfigurationBuilder().AddInMemoryCollection(memoryConfig).Build(); var logFactory = new LogFactory(); var logConfig = new NLogLoggingConfiguration(configuration.GetSection("NLog"), logFactory); logFactory.Configuration = logConfig; Assert.Equal("hello.txt", (logFactory.Configuration.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); logFactory.Configuration.Variables["var_filename"] = "updated.txt"; configuration.Reload(); // Automatic Reload Assert.Equal("updated.txt", (logFactory.Configuration.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); }
/// <summary> /// Setup dependency injection and create service provider /// </summary> /// <returns></returns> private IServiceProvider CreateServiceProvider() { var services = new ServiceCollection(); services.AddOperationsLibrary(); services.AddFileDataAccess(Configuration); var nlogConfig = new NLogLoggingConfiguration(Configuration.GetSection("NLog")); services.AddLogging(loggingBuilder => { loggingBuilder.ClearProviders(); loggingBuilder.SetMinimumLevel(LogLevel.Trace); loggingBuilder.AddNLog(nlogConfig); }); return(services.BuildServiceProvider()); }
private static IHostBuilder CreateHostBuilder() => new HostBuilder() .ConfigureAppConfiguration( (_, config) => { config.AddJsonFile("appsettings.json", false, false); config.AddEnvironmentVariables(prefix: "EDR_"); } ) .ConfigureServices( (context, services) => { var fs = new FileSystem(); services.AddSingleton <IFileSystem>(fs); services.AddConnectorManager(context.Configuration); services.AddSingleton <ConnectorCommand>(); services.AddSingleton <RunCommand>(); services.AddSingleton <StepsCommand>(); services.AddSingleton <ValidateCommand>(); services.AddSingleton <EDRMethods>(); var sclSettings = SCLSettings.CreateFromIConfiguration(context.Configuration); services.AddSingleton(sclSettings); } ) .ConfigureLogging( (context, logging) => { logging.ClearProviders(); logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); var nlogConfig = new NLogLoggingConfiguration(context.Configuration.GetSection("nlog")); LogManager.Configuration = nlogConfig; logging.AddNLog(nlogConfig); } );
private void ReloadLogFactoryConfiguration() { var memoryConfig = CreateMemoryConfigConsoleTargetAndRule(); memoryConfig["NLog:Targets:file:type"] = "File"; memoryConfig["NLog:Targets:file:fileName"] = "hello.txt"; memoryConfig["NLog:AutoReload"] = "true"; var configuration = new ConfigurationBuilder().AddInMemoryCollection(memoryConfig).Build(); var logFactory = new LogFactory(); var logConfig = new NLogLoggingConfiguration(configuration.GetSection("NLog"), logFactory); logFactory.Configuration = logConfig; Assert.Equal(2, logFactory.Configuration.LoggingRules[0].Targets.Count); configuration["NLog:Rules:0:writeTo"] = "Console"; logFactory.Configuration = logConfig.Reload(); // Manual Reload Assert.Equal(1, logFactory.Configuration.LoggingRules[0].Targets.Count); configuration["NLog:Rules:0:writeTo"] = "File,Console"; configuration.Reload(); // Automatic Reload Assert.Equal(2, logFactory.Configuration.LoggingRules[0].Targets.Count); logFactory.Dispose(); configuration.Reload(); // Nothing should happen }
public NLogLoggerFactory(NLogLoggingConfiguration config) { config.Setup(); }
private static ServiceProvider GetServiceProvider() { IServiceCollection serviceCollection = new ServiceCollection(); // Logging serviceCollection.AddLogging(loggingBuilder => { loggingBuilder.ClearProviders(); loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); loggingBuilder.AddNLog(); }); // Business Logic serviceCollection.AddSingleton <IDynamoDBContext>(new DynamoDBContext(new AmazonDynamoDBClient(RegionEndpoint.USEast1), new DynamoDBContextConfig() { ConsistentRead = true })) .AddTransient <IRequestBusinessLogic, RequestBusinessLogic>() .AddTransient <IRequestMapper, RequestMapper>() .AddTransient <ITokenUserData, TokenUserData>() .AddTransient <ITokenUserRepository, TokenUserRepository>() .AddTransient <ISkillProductsClientAdapter, SkillProductsClientAdapter>() .AddTransient <ISkillRequestValidator, SkillRequestValidator>() .AddTransient <IUserProfileClient, UserProfileClient>(); // SessionEndedRequest serviceCollection.AddTransient <IRequestRouter, SessionEndedRequestRouter>() .AddTransient <ISessionEndedRequestHandler, DefaultSessionEndedRequest>(); // ConnectionResponseRequest serviceCollection.AddTransient <IRequestRouter, ConnectionResponseRequestRouter>() .AddTransient <IConnectionResponseRequestHandler, DefaultConnectionResponseRequest>(); // LaunchRequest serviceCollection.AddTransient <IRequestRouter, LaunchRequestRouter>() .AddTransient <ILaunchRequestHandler, DefaultLaunchRequest>(); // IntentRequest serviceCollection.AddTransient <IRequestRouter, IntentRequestRouter>() .AddTransient <IIntentRequestHandler, AddAllPoints>() .AddTransient <IIntentRequestHandler, AddPlayer>() .AddTransient <IIntentRequestHandler, AddPoints>() .AddTransient <IIntentRequestHandler, AddSinglePoint>() .AddTransient <IIntentRequestHandler, DeleteAllPlayers>() .AddTransient <IIntentRequestHandler, DeletePlayer>() .AddTransient <IIntentRequestHandler, GetAllPlayersCount>() .AddTransient <IIntentRequestHandler, GetPlayerPoints>() .AddTransient <IIntentRequestHandler, GetPointsAverage>() .AddTransient <IIntentRequestHandler, GetPointsMax>() .AddTransient <IIntentRequestHandler, GetPointsMin>() .AddTransient <IIntentRequestHandler, ListAllPlayers>() .AddTransient <IIntentRequestHandler, ListAllPoints>() .AddTransient <IIntentRequestHandler, RemoveAllPoints>() .AddTransient <IIntentRequestHandler, RemovePoints>() .AddTransient <IIntentRequestHandler, RemoveSinglePoint>() .AddTransient <IIntentRequestHandler, ResetAllPoints>() .AddTransient <IIntentRequestHandler, Buy>() .AddTransient <IIntentRequestHandler, WhatCanIBuy>() .AddTransient <IIntentRequestHandler, Help>() .AddTransient <IIntentRequestHandler, RefundSubscription>() .AddTransient <IIntentRequestHandler, Fallback>() .AddTransient <IIntentRequestHandler, Stop>() .AddTransient <IIntentRequestHandler, Cancel>() .AddTransient <IIntentRequestHandler, NavigateHome>(); // Localization serviceCollection.AddLocalization(x => x.ResourcesPath = "Resources"); // Set logging configuration LoggingConfiguration nlogConfig = new NLogLoggingConfiguration(Configuration.File.GetSection("NLog")); LogManager.Configuration = nlogConfig; return(serviceCollection.BuildServiceProvider());; }