/// <summary> /// If there is no target in NLog.config defined a new one is registered /// with /// the default maxCount /// </summary> /// <param name="defaultMaxCount"></param> /// <returns></returns> public static MyCacheTarget GetInstance(int defaultMaxCount) { AppLoggingConfigHelper.EnsureLoggingConfigured( ); var target = ( MyCacheTarget )LogManager.Configuration.AllTargets.FirstOrDefault( t => t is MyCacheTarget ); if (target == null) { target = new MyCacheTarget { MaxCount = defaultMaxCount, Name = nameof(MyCacheTarget) }; LogManager.Configuration.AddTarget(target.Name, target); LogManager.Configuration.LoggingRules.Insert( 0 , new LoggingRule( "*" , LogLevel.FromString( "Trace" ) , target ) ); LogManager.ReconfigExistingLoggers( ); } return(target); }
/// <summary>Initializes a new instance of the <see cref="App"/> class.</summary> /// <param name="debugEventHandler">The debug event handler.</param> public App(EventHandler <DebugEventArgs> debugEventHandler = null) { //MappedDiagnosticsLogicalContext. if (debugEventHandler != null) { DebugMessage += debugEventHandler; } DebugLog = DoLogMessage; var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); // DoLogMessage( // folderPath // ); AppLoggingConfigHelper.EnsureLoggingConfigured( message => OnDebugMessage( new DebugEventArgs( message ) ) ); GlobalDiagnosticsContext.Set( "ExecutionContext" , new ExecutionContextImpl { Application = KayMcCormick .Logging.Common.Application .MainApplication } ); Logger = LogManager.LogFactory.GetCurrentClassLogger <MyLogger>(); ApplyConfiguration(); try { var s = new TaskCompletionSource <int>(); TCS = s; var cd = AppDomain.CurrentDomain; cd.AssemblyLoad += CurrentDomainOnAssemblyLoad; //cd.TypeResolve += CdOnTypeResolve; cd.ProcessExit += (sender, args) => { var argStr = args == null ? "Args is null" : args.ToString(); DebugLog($"Exiting. args is {argStr}"); }; cd.UnhandledException += OnAppDomainUnhandledException; cd.ResourceResolve += CdOnResourceResolve; cd.FirstChanceException += CurrentDomainOnFirstChanceException; } catch (Exception ex) { DebugLog(ex + "exception in constructor"); } }
private void _init( ) { AppLoggingConfigHelper.EnsureLoggingConfigured( ); var builder = new ContainerBuilder( ); builder.RegisterModule <IdGeneratorModule> ( ); builder.RegisterType <RandomClass> ( ).As <IRandom> ( ); Container = builder.Build( ); }
public override void Before([NotNull] MethodInfo methodUnderTest) { AppLoggingConfigHelper.EnsureLoggingConfigured( ); TestFileTarget = new FileTarget(Name); var fileTarget = TestFileTarget; fileTarget.FileName = Layout.FromString( "test-" + methodUnderTest.DeclaringType + "." + methodUnderTest.Name + ".txt" ); LogManager.LogFactory.Configuration.AddTarget(fileTarget); var loggingRule = new LoggingRule("*", LogLevel.Trace, fileTarget); TestLoggingRule = loggingRule; LogManager.LogFactory.Configuration.LoggingRules.Insert(0, loggingRule); LogManager.LogFactory.ReconfigExistingLoggers( ); }
/// <summary> /// Initializes a new instance of the <see cref="System.Object" /> /// class. /// </summary> public GlobalLoggingFixture(IMessageSink sink) { AppLoggingConfigHelper.EnsureLoggingConfigured( message => sink.OnMessage( new DiagnosticMessage( message ) ) ); var l = AppLoggingConfigHelper.SetupJsonLayout( ); sink.OnMessage( new DiagnosticMessage("Constructing GlobalLoggingFixture.") ); _xunitSinkTarget = new XunitSinkTarget("Xunitsink", sink) { Layout = l }; AppLoggingConfigHelper.AddTarget(_xunitSinkTarget, null); Logger.Warn($"{nameof ( GlobalLoggingFixture)} logger added."); }
public void TestEnsureConfigTwoArgs( ) { AppLoggingConfigHelper.EnsureLoggingConfigured( ); CheckLogConfig(LogManager.Configuration); }
/// <summary>Setups the container.</summary> /// <param name="container">The container.</param> /// <param name="assembliesToScan">The assemblies to scan.</param> /// <param name="containerHelperSettings"></param> /// <returns></returns> /// <autogeneratedoc /> /// TODO Edit XML Comment Template for SetupContainer public static ILifetimeScope SetupContainer( out IContainer container , IEnumerable <Assembly> assembliesToScan , ContainerHelperSettings containerHelperSettings ) { if (assembliesToScan == null) { assembliesToScan = GetAssembliesForScanningViaTypes( ); } if (containerHelperSettings != null) { Logger.Info("Applying container helper settings from app.config"); ApplySettings(containerHelperSettings); } else { Logger.Debug("No containerHelperSettings to apply."); } AppLoggingConfigHelper.EnsureLoggingConfigured( ); #if ENABLE_BUILDERPROXY BuilderInterceptor builderInterceptor = null; if (DoProxyBuilder) { Logger.Info("Proxying container builder for debug purposes."); var proxyGenerator = ProxyGenerator; builderInterceptor = new BuilderInterceptor(proxyGenerator); var proxy = proxyGenerator.CreateClassProxy <ContainerBuilder> (builderInterceptor); builder = proxy; } else { builder = new ContainerBuilder( ); } #else var builder = new ContainerBuilder( ); #endif // Set the property in order to propagate the settings. builder.Properties[InterceptProperty] = DoInterception; var toScan = assembliesToScan as Assembly[] ?? assembliesToScan.ToArray( ); builder.Properties[AssembliesForScanningProperty] = GetAssembliesForScanning( ); #region Autofac Modules builder.RegisterModule <AttributedMetadataModule> ( ); builder.RegisterModule <IdGeneratorModule> ( ); #if MENUS_ENABLE builder.RegisterModule < > ( ); #endif #endregion var i = 0; void LogStuff(ref int index) { #if ENABLE_BUILDERPROXY builderInterceptor?.Invocations.ForEach( invocation => Logger.Debug( $"{index}]: {invocation.Method.Name} ({string.Join ( ", " , invocation.Arguments )}) => {invocation.OriginalReturnValue}" ) ); #endif index++; } LogStuff(ref i); #region Assembly scanning builder.RegisterAssemblyTypes(toScan) .Where(MainScanningPredicate) .AsImplementedInterfaces( ); builder.RegisterAssemblyTypes(toScan.ToArray( )) .Where( delegate(Type t) { var isAssignableFrom = typeof(Window).IsAssignableFrom(t); TraceConditionalRegistration( t , typeof(Window) , isAssignableFrom ); return(isAssignableFrom); } ) .AsSelf( ) .As <Window> ( ) .OnActivating( args => { var argsInstance = args.Instance; if (argsInstance is IHaveLogger haveLogger) { haveLogger.Logger = args.Context.Resolve <ILogger> ( new TypedParameter( typeof (Type ) , argsInstance .GetType( ) ) ); } } ); #endregion #region Interceptors if (DoInterception) { builder.RegisterType <LoggingInterceptor> ( ).AsSelf( ); } #endregion #region Logging builder.RegisterType <LoggerTracker> ( ) .As <ILoggerTracker> ( ) .InstancePerLifetimeScope( ); // builder.RegisterType < LogFactory > ( ).AsSelf ( ) ; builder.Register( (c, p) => { var loggerName = "unset"; try { loggerName = p.TypedAs <Type> ( ).FullName; } catch (Exception ex) { Console.WriteLine(ex.ToString( )); } var tracker = c.Resolve <ILoggerTracker> ( ); Logger.Trace($"creating logger loggerName = {loggerName}"); var logger = LogManager.GetLogger(loggerName); tracker.TrackLogger(loggerName, logger); return(logger); } ) .As <ILogger> ( ); #endregion #region Callbacks builder.RegisterBuildCallback(c => Logger.Info("Container built.")); builder.RegisterCallback( registry => { registry.Registered += (sender, args) => { Logger.Trace( "Registered " + args.ComponentRegistration.Activator .LimitType ); args.ComponentRegistration.Activated += ( o , eventArgs ) => { Logger.Trace( $"Activated {DescribeComponent ( eventArgs.Component )} (sender={o}, instance={eventArgs.Instance})" ); }; }; } ); #endregion #region Container Build var setupContainer = builder.Build( ); container = setupContainer; setupContainer.ChildLifetimeScopeBeginning += SetupContainerOnChildLifetimeScopeBeginning; #endregion setupContainer.CurrentScopeEnding += SetupContainerOnCurrentScopeEnding; setupContainer.ResolveOperationBeginning += SetupContainerOnResolveOperationBeginning; var beginLifetimeScope = setupContainer.BeginLifetimeScope("initial scope"); return(beginLifetimeScope); }
/// <summary> /// Initializes a new instance of the <see cref="System.Object" /> /// class. /// </summary> public AppLoggerContainer( ) { AppLoggingConfigHelper.EnsureLoggingConfigured( ); }
/// <summary></summary> /// <param name="fixtureType"></param> /// <param name="lifecycle"></param> /// <autogeneratedoc /> /// TODO Edit XML Comment Template for LogFixtureLifecycleEvent public static void LogFixtureLifecycleEvent(Type fixtureType, Lifecycle lifecycle) { AppLoggingConfigHelper.EnsureLoggingConfigured(); new LogBuilder(Logger).Level(LogLevel.Trace).Message("Fixture lifecycle event for {fixtureType}: {lifecycle}", fixtureType, lifecycle).Write(); }