コード例 #1
0
        protected SignalrAppender AddSignalRAppender(string groupName)
        {
            var signalrAppender = new SignalrAppender(SignalrHub, groupName)
            {
                Name      = Name + "SignalrAppender_" + groupName,
                Threshold = Level.All
            };

            var signalrLayout = new PatternLayout
            {
                ConversionPattern = "%d - %m%n%n"
            };

            signalrLayout.ActivateOptions();
            signalrAppender.Layout = signalrLayout;

            signalrAppender.ActivateOptions();

            _logger.AddAppender(signalrAppender);

            return(signalrAppender);
        }
コード例 #2
0
        protected void ConfigureAppenders(string fileName)
        {
            //Get the logger repository hierarchy.
            if (!(LogManager.GetRepository(Assembly.GetCallingAssembly()) is Hierarchy repository))
            {
                throw new Exception("log4net repository was not configured");
            }

            if (!(repository.GetLogger(_loggerName) is Logger logger))
            {
                throw new Exception($"Logger '{_loggerName}' not found");
            }

            #region Configure signalRAppender

            if (logger.Appenders.OfType <SignalrAppender>().All(a => a.GroupName != _logGroupName))
            {
                //var hub = GlobalHost.ConnectionManager.GetHubContext<SignalrAppenderHub>();
                var hubContext      = _iocResolver.Resolve <IHubContext <SignalrAppenderHub> >();
                var signalrAppender = new SignalrAppender(hubContext, _logGroupName)
                {
                    Name      = _loggerName + "SignalrAppenderAuto",
                    Threshold = Level.All,
                };

                var signalrLayout = new PatternLayout()
                {
                    ConversionPattern = "%d - %m%n%n"
                };

                signalrLayout.ActivateOptions();
                signalrAppender.Layout = signalrLayout;
                signalrAppender.ActivateOptions();

                logger.AddAppender(signalrAppender);
            }

            #endregion

            #region Configure file appender

            if (!string.IsNullOrWhiteSpace(fileName))
            {
                var fileAppender = new FileAppender
                {
                    Name         = _loggerName + "FileAppenderAuto",
                    File         = fileName,
                    AppendToFile = false,
                    Threshold    = Level.All,
                    LockingModel = new FileAppender.MinimalLock()
                };
                //new log4net.Util.PatternString("D:\Temp\Logs\%property{LogName}.log")

                var fileLayout = new PatternLayout()
                {
                    ConversionPattern = "%d - %m%n%n"
                };

                fileLayout.ActivateOptions();
                fileAppender.Layout = fileLayout;
                fileAppender.ActivateOptions();

                logger.AddAppender(fileAppender);
            }

            #endregion

            // Mark repository as configured and notify that is has changed.
            repository.Configured = true;
            repository.RaiseConfigurationChanged(EventArgs.Empty);

            _logger = LogManager.GetLogger(Assembly.GetCallingAssembly(), _loggerName);
        }