/// <summary>Initializes a new instance of the <see cref="LogFactoryInterceptor"/> class.</summary>
 /// <param name="generator">The generator.</param>
 /// <param name="useLogMethod">The use log method.</param>
 /// <autogeneratedoc />
 /// TODO Edit XML Comment Template for #ctor
 public LogFactoryInterceptor(
     ProxyGenerator generator
     , LogDelegates.LogMethod useLogMethod
     )
 {
     Generator    = generator;
     UseLogMethod = useLogMethod;
 }
        internal static void ConfigureLogging(
            LogDelegates.LogMethod logMethod
            , bool proxyLogging = false
            )
        {
            // logMethod (
            // Resource
            // .AppLoggingConfigHelper_ConfigureLogging_____Starting_logger_configuration_
            // ) ;

            LogFactory proxiedFactory = null;

            if (proxyLogging)
            {
                var proxyGenerator    = new ProxyGenerator();
                var loggerProxyHelper = new LoggerProxyHelper(proxyGenerator, DoLogMessage);
                var logFactory        = new MyLogFactory(DoLogMessage);
                var lConfLogFactory   = loggerProxyHelper.CreateLogFactory(logFactory);
                proxiedFactory = lConfLogFactory;
            }

            var fieldInfo = typeof(LogManager).GetField(
                "factory"
                , BindingFlags.Static
                | BindingFlags.NonPublic
                );

            if (fieldInfo != null)
            {
                logMethod($"field info is {fieldInfo.DeclaringType} . {fieldInfo.Name}");
                var cur = fieldInfo.GetValue(null);
                logMethod($"cur is {cur}");

                if (proxyLogging)
                {
                    fieldInfo.SetValue(null, proxiedFactory);
                    var newVal = fieldInfo.GetValue(null);
                    logMethod($"New Value = {newVal}");
                }
            }

            // var useFactory = proxyLogging ? proxiedFactory : LogManager.LogFactory;
            var lConf = new CodeConfiguration();

            var dict = LogLevel.AllLoggingLevels.ToDictionary(
                level => level
                , level => new List <Target>()
                );
            var errorTargets = dict[LogLevel.Error];
            var t            = dict[LogLevel.Trace];
            // var x = new EventLogTarget("eventLog") { Source = "Application Error" };
            // errorTargets.Add(x);

            var webServiceTarget = new LogReceiverWebServiceTarget("log");

            webServiceTarget.EndpointAddress = "http://xx1.mynetgear.com/LogService/ReceiveLogs.svc";// "http://localhost:27809/ReceiveLogs.svc";
            // webServiceTarget.EndpointConfigurationName = "log";
            dict[LogLevel.Debug].Add(webServiceTarget);



            #region Cache Target
#if false
            var cacheTarget = new  MyCacheTarget( );
            t.Add(cacheTarget);
#endif
            #endregion
            #region NLogViewer Target
            var viewer = Viewer();
            t.Add(viewer);
            #endregion
            #region Debugger Target
            if (DebuggerTargetEnabled)
            {
                var debuggerTarget =
                    new DebuggerTarget {
                    Layout = new SimpleLayout("${message}")
                };
                t.Add(debuggerTarget);
            }
            #endregion
            #region Chainsaw Target
            var chainsawTarget = new ChainsawTarget( );
            SetupNetworkTarget(chainsawTarget, "udp://xx1.mynetgear.com:4445");
            t.Add(chainsawTarget);
            #endregion
            t.Add(MyFileTarget());
            var jsonFileTarget = JsonFileTarget();
            t.Add(jsonFileTarget);
            var byType = new Dictionary <Type, int>();

            foreach (var target in dict.SelectMany(pair => pair.Value))
            {
                var type = target.GetType();
                byType.TryGetValue(type, out var count);
                count       += 1;
                byType[type] = count;

                if (target.Name == null)
                {
                    target.Name = $"{Regex.Replace(type.Name, "Target", "")}{count:D2}";
                }

                lConf.AddTarget(target);
            }

            foreach (var result in dict.Select(LoggingRule))
            {
                ((List <LoggingRule>)lConf.LoggingRules).AddRange(result);
            }

            LogManager.Configuration = lConf;
            Logger = LogManager.GetCurrentClassLogger();
        }
        public static void EnsureLoggingConfigured(
            LogDelegates.LogMethod logMethod         = null
            , [CallerFilePath] string callerFilePath = null
            )
        {
            if (!_numTimesConfigured.HasValue)
            {
                _numTimesConfigured = 1;
            }
            else
            {
                _numTimesConfigured += 1;
            }

            if (logMethod == null)
            {
                logMethod = DoLogMessage;
            }

            logMethod(
                $"[time {_numTimesConfigured.Value}]\t{nameof(EnsureLoggingConfigured)} called from {callerFilePath}"
                );


            var f = typeof(LogManager).GetField(
                "factory"
                , BindingFlags.Static | BindingFlags.NonPublic
                );
            bool configLoaded = false;

            if (f != null)
            {
                LogFactory fc         = ( LogFactory )f.GetValue(null);
                var        fieldInfo2 = fc.GetType()
                                        .GetField(
                    "_config"
                    , BindingFlags.Instance | BindingFlags.NonPublic
                    );

                if (fieldInfo2 == null)
                {
                    System.Diagnostics.Debug.WriteLine("no field _config for " + fc);
                    // throw new Exception ( Resources.AppLoggingConfigHelper_EnsureLoggingConfigured_no_config_loaded_field_found ) ;
                }

                if (fieldInfo2 != null)
                {
                    var config = fieldInfo2.GetValue(fc);

                    //LogManager.ThrowConfigExceptions = true;
                    //LogManager.ThrowExceptions = true;
                    var fieldInfo = fc.GetType( )
                                    .GetField(
                        "_configLoaded"
                        , BindingFlags.Instance | BindingFlags.NonPublic
                        );

                    if (fieldInfo == null)
                    {
                        configLoaded = config != null;

                        System.Diagnostics.Debug.WriteLine("no field _configLoaded for " + fc);
                        // throw new Exception ( "no config loaded field found" ) ;
                    }
                    else
                    {
                        configLoaded = ( bool )fieldInfo.GetValue(fc);
                    }
                }

                LoggingIsConfigured = configLoaded;
                var isMyConfig =
                    !configLoaded || LogManager.Configuration is CodeConfiguration;
                var doConfig = !LoggingIsConfigured || ForceCodeConfig && !isMyConfig;
                logMethod(
                    $"{nameof(LoggingIsConfigured)} = {LoggingIsConfigured}; {nameof(ForceCodeConfig)} = {ForceCodeConfig}; {nameof(isMyConfig)} = {isMyConfig});"
                    );
                if (DumpExistingConfig)
                {
                    void Collect(string s)
                    {
                        System.Diagnostics.Debug.WriteLine(s);
                    }

                    DoDumpConfig(Collect);
                }

                if (doConfig)
                {
                    ConfigureLogging(logMethod);
                    return;
                }
            }

            DumpPossibleConfig(LogManager.Configuration);
        }
 /// <summary>Initializes a new instance of the <see cref="LoggerFactoryHook"/> class.</summary>
 /// <param name="useLogMethod">The use log method.</param>
 /// <autogeneratedoc />
 /// TODO Edit XML Comment Template for #ctor
 public LoggerFactoryHook(LogDelegates.LogMethod useLogMethod)
 {
     _useLogMethod = useLogMethod;
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="LoggerInterceptor" />
 ///     class.
 /// </summary>
 /// <param name="useLogMethod">The use log method.</param>
 /// <autogeneratedoc />
 /// TODO Edit XML Comment Template for #ctor
 public LoggerInterceptor(LogDelegates.LogMethod useLogMethod)
 {
     UseLogMethod = useLogMethod;
 }
Example #6
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="System.Object" />
 ///     class.
 /// </summary>
 public LoggerProxyHelper(ProxyGenerator generator, LogDelegates.LogMethod logMethod)
 {
     Generator    = generator;
     UseLogMethod = logMethod;
 }
Example #7
0
 /// <summary>Initializes a new instance of the <see cref="MyLogFactory"/> class.</summary>
 /// <param name="doLogMessage">The do log message.</param>
 /// <autogeneratedoc />
 /// TODO Edit XML Comment Template for #ctor
 public MyLogFactory(LogDelegates.LogMethod doLogMessage)
 {
     _doLogMessage = doLogMessage;
 }