private Target WrapWithDefaultWrapper(Target t, XmlElement defaultWrapperElement)
        {
            string            wrapperType           = GetCaseInsensitiveAttribute(defaultWrapperElement, "type");
            Target            wrapperTargetInstance = TargetFactory.CreateTarget(wrapperType);
            WrapperTargetBase wtb = wrapperTargetInstance as WrapperTargetBase;

            if (wtb == null)
            {
                throw new NLogConfigurationException("Target type specified on <default-wrapper /> is not a wrapper.");
            }
            ConfigureTargetFromXmlElement(wrapperTargetInstance, defaultWrapperElement);
            while (wtb.WrappedTarget != null)
            {
                wtb = wtb.WrappedTarget as WrapperTargetBase;
                if (wtb == null)
                {
                    throw new NLogConfigurationException("Child target type specified on <default-wrapper /> is not a wrapper.");
                }
            }
            wtb.WrappedTarget          = t;
            wrapperTargetInstance.Name = t.Name;
            t.Name = t.Name + "_wrapped";

            InternalLogger.Debug("Wrapping target '{0}' with '{1}' and renaming to '{2}", wrapperTargetInstance.Name, wrapperTargetInstance.GetType().Name, t.Name);
            return(wrapperTargetInstance);
        }
        private Target WrapWithDefaultWrapper(Target t, NLogXmlElement defaultParameters)
        {
            string wrapperType = StripOptionalNamespacePrefix(defaultParameters.GetRequiredAttribute("type"));

            Target            wrapperTargetInstance = this.ConfigurationItemFactory.Targets.CreateInstance(wrapperType);
            WrapperTargetBase wtb = wrapperTargetInstance as WrapperTargetBase;

            if (wtb == null)
            {
                throw new NLogConfigurationException("Target type specified on <default-wrapper /> is not a wrapper.");
            }

            this.ParseTargetElement(wrapperTargetInstance, defaultParameters);
            while (wtb.WrappedTarget != null)
            {
                wtb = wtb.WrappedTarget as WrapperTargetBase;
                if (wtb == null)
                {
                    throw new NLogConfigurationException("Child target type specified on <default-wrapper /> is not a wrapper.");
                }
            }

            wtb.WrappedTarget          = t;
            wrapperTargetInstance.Name = t.Name;
            t.Name = t.Name + "_wrapped";

            InternalLogger.Debug("Wrapping target '{0}' with '{1}' and renaming to '{2}", wrapperTargetInstance.Name, wrapperTargetInstance.GetType().Name, t.Name);
            return(wrapperTargetInstance);
        }
        public static Target ReturnTarget(string targetName)
        {
            Target test = null;

            if (LogManager.Configuration != null && LogManager.Configuration.ConfiguredNamedTargets.Count != 0)
            {
                Target target = LogManager.Configuration.FindTargetByName(targetName);
                if (target == null)
                {
                    throw new Exception("Could not find target named: " + targetName);
                }

                WrapperTargetBase wrapperTarget = target as WrapperTargetBase;

                // Unwrap the target if necessary.
                if (wrapperTarget == null)
                {
                    test = target;
                }
                else
                {
                    test = wrapperTarget.WrappedTarget;
                }

                if (target == null)
                {
                    throw new Exception("Could not get a Target from " + target.GetType());
                }
            }
            else
            {
                throw new Exception("LogManager contains no Configuration or there are no named targets");
            }
            return(test);
        }
Exemple #4
0
        public TransmissionLoggingQueue()
        {
            _log  = _serverSettings.GetGeneralSetting(ServerSettingsKeys.TRANSMISSION_LOG_ENABLED).BoolValue;
            _stop = false;

            WrapperTargetBase b = (WrapperTargetBase)LogManager.Configuration.FindTargetByName("asyncTransmissionFileTarget");

            _fileTarget = b != null ? (FileTarget)b.WrappedTarget : null;
            //_fileTarget = (FileTarget)b.WrappedTarget;
        }
Exemple #5
0
        public IActionResult GetLogRules()
        {
            // Checks the request is valid.
            if (!this.ModelState.IsValid)
            {
                return(ModelStateErrors.BuildErrorResponse(this.ModelState));
            }

            try
            {
                var rules = new List <LogRuleModel>();

                foreach (LoggingRule rule in LogManager.Configuration.LoggingRules)
                {
                    string filename = string.Empty;

                    if (!rule.Targets.Any())
                    {
                        continue;
                    }

                    // Retrieve the full path of the current rule's log file.
                    if (rule.Targets.First().GetType().Name == "AsyncTargetWrapper")
                    {
                        WrapperTargetBase wrapper = (WrapperTargetBase)rule.Targets.First();

                        if (wrapper.WrappedTarget != null && wrapper.WrappedTarget.GetType().Name == "FileTarget")
                        {
                            filename = ((FileTarget)wrapper.WrappedTarget).FileName.ToString();
                        }
                    }
                    else if (rule.Targets.First().GetType().Name == "FileTarget")
                    {
                        filename = ((FileTarget)rule.Targets.First()).FileName.ToString();
                    }

                    rules.Add(new LogRuleModel
                    {
                        RuleName = rule.LoggerNamePattern,
                        LogLevel = rule.Levels.First().Name,
                        Filename = filename
                    });
                }

                return(this.Json(rules));
            }
            catch (Exception e)
            {
                this.logger.LogError("Exception occurred: {0}", e.ToString());
                return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString()));
            }
        }
Exemple #6
0
        private void LogCompleteTransmissions()
        {
            while (!_stop)
            {
                Thread.Sleep(500);
                if (_log != !_serverSettings.GetGeneralSetting(ServerSettingsKeys.TRANSMISSION_LOG_ENABLED).BoolValue)
                {
                    _log = !_serverSettings.GetGeneralSetting(ServerSettingsKeys.TRANSMISSION_LOG_ENABLED).BoolValue;
                    string newSetting = _log ? "TRANSMISSION LOGGING ENABLED" : "TRANSMISSION LOGGING DISABLED";

                    if (_serverSettings.GetGeneralSetting(ServerSettingsKeys.TRANSMISSION_LOG_ENABLED).BoolValue &&
                        _fileTarget == null)    // require initialization of transmission logging filetarget and rule
                    {
                        LoggingConfiguration config = LogManager.Configuration;

                        config = LoggingHelper.GenerateTransmissionLoggingConfig(config,
                                                                                 _serverSettings.GetGeneralSetting(ServerSettingsKeys.TRANSMISSION_LOG_RETENTION).IntValue);

                        LogManager.Configuration = config;

                        WrapperTargetBase b = (WrapperTargetBase)LogManager.Configuration.FindTargetByName("asyncTransmissionFileTarget");
                        _fileTarget = (FileTarget)b.WrappedTarget;
                    }

                    Logger.Info($"EVENT, {newSetting}");
                }

                if (_serverSettings.GetGeneralSetting(ServerSettingsKeys.TRANSMISSION_LOG_ENABLED).BoolValue&&
                    _fileTarget.MaxArchiveFiles != _serverSettings.GetGeneralSetting(ServerSettingsKeys.TRANSMISSION_LOG_RETENTION).IntValue)
                {
                    _fileTarget.MaxArchiveFiles = _serverSettings.GetGeneralSetting(ServerSettingsKeys.TRANSMISSION_LOG_RETENTION).IntValue;
                    LogManager.ReconfigExistingLoggers();
                }

                if (_log && !_currentTransmissionLog.IsEmpty)
                {
                    foreach (KeyValuePair <SRClient, TransmissionLog> LoggedTransmission in _currentTransmissionLog)
                    {
                        if (LoggedTransmission.Value.IsComplete())
                        {
                            if (_currentTransmissionLog.TryRemove(LoggedTransmission.Key, out TransmissionLog completedLog))
                            {
                                Logger.Info($"TRANSMISSION, {LoggedTransmission.Key.ClientGuid}, {LoggedTransmission.Key.Name}, " +
                                            $"{LoggedTransmission.Key.Coalition}, {LoggedTransmission.Value.TransmissionFrequency}. " +
                                            $"{completedLog.TransmissionStart}, {completedLog.TransmissionEnd}, {LoggedTransmission.Key.VoipPort}");
                            }
                        }
                    }
                }
            }
        }
Exemple #7
0
        public static string GetLogFileName(string targetName)
        {
            string fileName = null;

            if (LogManager.Configuration != null && LogManager.Configuration.ConfiguredNamedTargets.Count != 0)
            {
                Target target = LogManager.Configuration.FindTargetByName(targetName);
                if (target == null)
                {
                    throw new Exception("Could not find target named: " + targetName);
                }

                FileTarget        fileTarget    = null;
                WrapperTargetBase wrapperTarget = target as WrapperTargetBase;

                // Unwrap the target if necessary.
                if (wrapperTarget == null)
                {
                    fileTarget = target as FileTarget;
                }
                else
                {
                    fileTarget = wrapperTarget.WrappedTarget as FileTarget;
                }

                if (fileTarget == null)
                {
                    throw new Exception("Could not get a FileTarget from " + target.GetType());
                }

                var logEventInfo = new LogEventInfo {
                    TimeStamp = DateTime.Now
                };
                fileName = fileTarget.FileName.Render(logEventInfo);
            }
            else
            {
                throw new Exception("LogManager contains no Configuration or there are no named targets");
            }

            if (!File.Exists(fileName))
            {
                throw new Exception("File " + fileName + " does not exist");
            }

            return(fileName);
        }
Exemple #8
0
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBoxFilePath.Text))
            {
                if (LogManager.Configuration != null && LogManager.Configuration.ConfiguredNamedTargets.Count != 0)
                {
                    Target target = LogManager.Configuration.FindTargetByName("file");
                    if (target == null)
                    {
                        throw new Exception("Could not find target named: " + "file");
                    }

                    FileTarget        fileTarget    = null;
                    WrapperTargetBase wrapperTarget = target as WrapperTargetBase;

                    // Unwrap the target if necessary.
                    if (wrapperTarget == null)
                    {
                        fileTarget = target as FileTarget;
                    }
                    else
                    {
                        fileTarget = wrapperTarget.WrappedTarget as FileTarget;
                    }

                    if (fileTarget == null)
                    {
                        throw new Exception("Could not get a FileTarget from " + target.GetType());
                    }

                    fileTarget.FileName = textBoxFilePath.Text;
                    LogManager.ReconfigExistingLoggers();

                    var nlogConfigFile = "NLog.config";
                    var xdoc           = XDocument.Load(nlogConfigFile);
                    var ns             = xdoc.Root.GetDefaultNamespace();
                    var fTarget        = xdoc.Descendants(ns + "target")
                                         .FirstOrDefault(t => (string)t.Attribute("name") == "file");
                    fTarget.SetAttributeValue("fileName", textBoxFilePath.Text);
                    xdoc.Save(nlogConfigFile);

                    this.Close();
                }
            }
        }
        public string GetCurrentLogFileName()
        {
            string fileName = null;

            if (LogManager.Configuration != null && LogManager.Configuration.ConfiguredNamedTargets.Count != 0)
            {
                Target target = LogManager.Configuration.FindTargetByName("NLogAsyncWrapper");
                if (target == null)
                {
                    throw new Exception("Could not find target named: " + "NLogAsyncWrapper");
                }

                FileTarget        fileTarget    = null;
                WrapperTargetBase wrapperTarget = target as WrapperTargetBase;

                // Unwrap the target if necessary.
                if (wrapperTarget == null)
                {
                    fileTarget = target as FileTarget;
                }
                else
                {
                    fileTarget = wrapperTarget.WrappedTarget as FileTarget;
                }

                if (fileTarget == null)
                {
                    throw new Exception("Could not get a FileTarget from " + target.GetType());
                }

                var logEventInfo = new LogEventInfo {
                    TimeStamp = DateTime.Now
                };
                fileName = fileTarget.FileName.Render(logEventInfo);
            }
            else
            {
                throw new Exception("LogManager contains no Configuration or there are no named targets");
            }

            this._Filename = fileName; //refresh

            return(fileName);
        }
Exemple #10
0
        private void FormChangeLog_Load(object sender, EventArgs e)
        {
            if (LogManager.Configuration != null && LogManager.Configuration.ConfiguredNamedTargets.Count != 0)
            {
                Target target = LogManager.Configuration.FindTargetByName("file");

                FileTarget        fileTarget    = null;
                WrapperTargetBase wrapperTarget = target as WrapperTargetBase;

                // Unwrap the target if necessary.
                if (wrapperTarget == null)
                {
                    fileTarget = target as FileTarget;
                }
                else
                {
                    fileTarget = wrapperTarget.WrappedTarget as FileTarget;
                }

                textBoxFilePath.Text = fileTarget.FileName.ToString().Replace("\'", "");
            }
        }
        private static FallbackGroupTarget CreateAndInitializeFallbackGroupTarget(bool returnToFirstOnSuccess, params Target[] targets)
        {
            var wrapper = new FallbackGroupTarget(targets)
            {
                ReturnToFirstOnSuccess = returnToFirstOnSuccess,
            };

            foreach (var target in targets)
            {
                WrapperTargetBase wrapperTarget = target as WrapperTargetBase;
                if (wrapperTarget != null)
                {
                    wrapperTarget.WrappedTarget.Initialize(null);
                }

                target.Initialize(null);
            }

            wrapper.Initialize(null);

            return(wrapper);
        }
        private static Exception WriteNumberAsyncLogEventsStartingAt(int startIndex, int count, WrapperTargetBase wrapper)
        {
            Exception lastException = null;

            for (int i = startIndex; i < startIndex + count; i++)
            {
                wrapper.WriteAsyncLogEvent(
                    new LogEventInfo(LogLevel.Debug, "test", $"Hello {i}").WithContinuation(ex => lastException = ex));
            }
            return(lastException);
        }