private static void RegisterTraceIdGenerator(this ContainerBuilder builder) { builder.RegisterType <FallbackTraceIdAccessor>().As <FallbackTraceIdAccessor>().InstancePerRequest(); if (!ExpectedTraceIdGeneratorName.HasValue()) { builder.Register(__traceIdGeneratorFactory).As <ILogTraceIdGenerator>().InstancePerRequest(); ExpectedTraceIdGeneratorName.Value = nameof(SystemTraceIdGenerator); } ILogTraceIdGenerator __traceIdGeneratorFactory(IComponentContext provider) { //1. Get traceIdAccessor and fallbackTraceIdAccessor from ServiceProvider var traceIdAccessor = provider.ResolveOptional <TraceIdAccessor>(); var fallbackAccessor = provider.Resolve <FallbackTraceIdAccessor>(); //2. Create a new instance of SystemTraceIdGenerator var generator = new SystemTraceIdGenerator(traceIdAccessor, fallbackAccessor); //3. Scoped update LogTraceIdGenerator.ScopedUpdate(generator); //4. Done, and return. return(generator); } }
private static void RegisterTraceIdGenerator(this IContainer ioc) { ioc.Register <FallbackTraceIdAccessor, FallbackTraceIdAccessor>(ReuseType.Scoped); if (!ExpectedTraceIdGeneratorName.HasValue()) { ioc.RegisterDelegate(typeof(ILogTraceIdGenerator), () => __traceIdGeneratorFactory(ioc), ReuseType.Scoped); ExpectedTraceIdGeneratorName.Value = nameof(SystemTraceIdGenerator); } ILogTraceIdGenerator __traceIdGeneratorFactory(IContainer provider) { //1. Get traceIdAccessor and fallbackTraceIdAccessor from ServiceProvider var traceIdAccessor = provider.Resolve <TraceIdAccessor>(IfUnresolved.ReturnDefault); var fallbackAccessor = provider.Resolve <FallbackTraceIdAccessor>(); //2. Create a new instance of SystemTraceIdGenerator var generator = new SystemTraceIdGenerator(traceIdAccessor, fallbackAccessor); //3. Scoped update LogTraceIdGenerator.ScopedUpdate(generator); //4. Done, and return. return(generator); } }
/// <summary> /// Register System TraceId generator for Autofac /// </summary> /// <param name="services"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public static ILogServiceCollection RegisterSystemTraceIdGenerator(this ILogServiceCollection services) { if (services is null) { throw new ArgumentNullException(nameof(services)); } if (!ExpectedTraceIdGeneratorName.HasValue()) { services.AddDependency(s => s.AddScoped <ILogTraceIdGenerator, IComponentContext>(__traceIdGeneratorFactory)); ExpectedTraceIdGeneratorName.Value = nameof(SystemTraceIdGenerator); } // ReSharper disable once InconsistentNaming ILogTraceIdGenerator __traceIdGeneratorFactory(IComponentContext provider) { //1. Get traceIdAccessor and fallbackTraceIdAccessor from ServiceProvider var traceIdAccessor = provider.ResolveOptional <TraceIdAccessor>(); var fallbackAccessor = provider.Resolve <FallbackTraceIdAccessor>(); //2. Create a new instance of SystemTraceIdGenerator var generator = new SystemTraceIdGenerator(traceIdAccessor, fallbackAccessor); //3. Scoped update LogTraceIdGenerator.ScopedUpdate(generator); //4. Done, and return. return(generator); } return(services); }
private static void AddTraceIdGenerator(this ILogServiceCollection services) { services.AddDependency(s => s.TryAddScoped <FallbackTraceIdAccessor, FallbackTraceIdAccessor>()); if (!ExpectedTraceIdGeneratorName.HasValue()) { services.AddDependency(s => s.TryAddScoped <ILogTraceIdGenerator, IServiceProvider>(__traceIdGeneratorFactory)); ExpectedTraceIdGeneratorName.Value = nameof(SystemTraceIdGenerator); } // ReSharper disable once InconsistentNaming ILogTraceIdGenerator __traceIdGeneratorFactory(IServiceProvider provider) { //1. Get traceIdAccessor and fallbackTraceIdAccessor from ServiceProvider var traceIdAccessor = provider.GetService <TraceIdAccessor>(); var fallbackAccessor = provider.GetRequiredService <FallbackTraceIdAccessor>(); //2. Create a new instance of SystemTraceIdGenerator var generator = new SystemTraceIdGenerator(traceIdAccessor, fallbackAccessor); //3. Scoped update LogTraceIdGenerator.ScopedUpdate(generator); //4. Done, and return. return(generator); } }
private static void AddTraceIdGenerator(this IServiceCollection services) { services.TryAdd(ServiceDescriptor.Scoped <FallbackTraceIdAccessor, FallbackTraceIdAccessor>()); if (!ExpectedTraceIdGeneratorName.HasValue()) { services.TryAdd(ServiceDescriptor.Scoped <ILogTraceIdGenerator, AspNetCoreTraceIdGenerator>()); ExpectedTraceIdGeneratorName.Value = nameof(AspNetCoreTraceIdGenerator); } }
/// <summary> /// Register Microsoft ASP.NET Core Trace Id generator /// </summary> /// <param name="services"></param> /// <returns></returns> public static ILogServiceCollection RegisterAspNetCoreTraceIdGenerator(this ILogServiceCollection services) { if (!ExpectedTraceIdGeneratorName.HasValue()) { services.AddDependency(s => s.AddScoped <ILogTraceIdGenerator, AspNetCoreTraceIdGenerator>()); ExpectedTraceIdGeneratorName.Value = nameof(AspNetCoreTraceIdGenerator); } return(services); }