本文整理汇总了C#中NLog.Config.NLogXmlElement类的典型用法代码示例。如果您正苦于以下问题:C# NLogXmlElement类的具体用法?C# NLogXmlElement怎么用?C# NLogXmlElement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NLogXmlElement类属于NLog.Config命名空间,在下文中一共展示了NLogXmlElement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureObjectFromAttributes
private void ConfigureObjectFromAttributes(object targetObject, NLogXmlElement element, bool ignoreType)
{
var attributeValues = element.AttributeValues.ToList();
foreach (var kvp in attributeValues)
{
string childName = kvp.Key;
string childValue = kvp.Value;
if (ignoreType && childName.Equals("type", StringComparison.OrdinalIgnoreCase))
{
continue;
}
try
{
PropertyHelper.SetPropertyFromString(targetObject, childName, this.ExpandSimpleVariables(childValue), this.ConfigurationItemFactory);
}
catch (NLogConfigurationException)
{
InternalLogger.Warn("Error when setting '{0}' on attibute '{1}'", childValue, childName);
throw;
}
}
}
示例2: ParseIncludeElement
private void ParseIncludeElement(NLogXmlElement includeElement, string baseDirectory, bool autoReloadDefault)
{
includeElement.AssertName("include");
string newFileName = includeElement.GetRequiredAttribute("file");
try
{
newFileName = this.ExpandSimpleVariables(newFileName);
newFileName = SimpleLayout.Evaluate(newFileName);
if (baseDirectory != null)
{
newFileName = Path.Combine(baseDirectory, newFileName);
}
#if SILVERLIGHT
newFileName = newFileName.Replace("\\", "/");
if (Application.GetResourceStream(new Uri(newFileName, UriKind.Relative)) != null)
#else
if (File.Exists(newFileName))
#endif
{
InternalLogger.Debug("Including file '{0}'", newFileName);
this.ConfigureFromFile(newFileName, autoReloadDefault);
}
else
{
throw new FileNotFoundException("Included file not found: " + newFileName);
}
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
InternalLogger.Error("Error when including '{0}' {1}", newFileName, exception);
if (includeElement.GetOptionalBooleanAttribute("ignoreErrors", false))
{
return;
}
throw new NLogConfigurationException("Error when including: " + newFileName, exception);
}
}
示例3: SetPropertyFromElement
private void SetPropertyFromElement(object o, NLogXmlElement element)
{
if (this.AddArrayItemFromElement(o, element))
{
return;
}
if (this.SetLayoutFromElement(o, element))
{
return;
}
PropertyHelper.SetPropertyFromString(o, element.LocalName, this.ExpandSimpleVariables(element.Value), this.ConfigurationItemFactory);
}
示例4: ParseVariableElement
private void ParseVariableElement(NLogXmlElement variableElement)
{
variableElement.AssertName("variable");
string name = variableElement.GetRequiredAttribute("name");
string value = this.ExpandSimpleVariables(variableElement.GetRequiredAttribute("value"));
this.Variables[name] = value;
}
示例5: ParseTargetElement
private void ParseTargetElement(Target target, NLogXmlElement targetElement)
{
var compound = target as CompoundTargetBase;
var wrapper = target as WrapperTargetBase;
this.ConfigureObjectFromAttributes(target, targetElement, true);
foreach (var childElement in targetElement.Children)
{
string name = childElement.LocalName;
if (compound != null)
{
if (IsTargetRefElement(name))
{
string targetName = childElement.GetRequiredAttribute("name");
Target newTarget = this.FindTargetByName(targetName);
if (newTarget == null)
{
throw new NLogConfigurationException("Referenced target '" + targetName + "' not found.");
}
compound.Targets.Add(newTarget);
continue;
}
if (IsTargetElement(name))
{
string type = StripOptionalNamespacePrefix(childElement.GetRequiredAttribute("type"));
Target newTarget = this.ConfigurationItemFactory.Targets.CreateInstance(type);
if (newTarget != null)
{
this.ParseTargetElement(newTarget, childElement);
if (newTarget.Name != null)
{
// if the new target has name, register it
AddTarget(newTarget.Name, newTarget);
}
compound.Targets.Add(newTarget);
}
continue;
}
}
if (wrapper != null)
{
if (IsTargetRefElement(name))
{
string targetName = childElement.GetRequiredAttribute("name");
Target newTarget = this.FindTargetByName(targetName);
if (newTarget == null)
{
throw new NLogConfigurationException("Referenced target '" + targetName + "' not found.");
}
wrapper.WrappedTarget = newTarget;
continue;
}
if (IsTargetElement(name))
{
string type = StripOptionalNamespacePrefix(childElement.GetRequiredAttribute("type"));
Target newTarget = this.ConfigurationItemFactory.Targets.CreateInstance(type);
if (newTarget != null)
{
this.ParseTargetElement(newTarget, childElement);
if (newTarget.Name != null)
{
// if the new target has name, register it
AddTarget(newTarget.Name, newTarget);
}
if (wrapper.WrappedTarget != null)
{
throw new NLogConfigurationException("Wrapped target already defined.");
}
wrapper.WrappedTarget = newTarget;
}
continue;
}
}
this.SetPropertyFromElement(target, childElement);
}
}
示例6: ParseNLogElement
/// <summary>
/// Parse {NLog} xml element.
/// </summary>
/// <param name="nlogElement"></param>
/// <param name="filePath">path to config file.</param>
/// <param name="autoReloadDefault">The default value for the autoReload option.</param>
private void ParseNLogElement(NLogXmlElement nlogElement, string filePath, bool autoReloadDefault)
{
InternalLogger.Trace("ParseNLogElement");
nlogElement.AssertName("nlog");
if (nlogElement.GetOptionalBooleanAttribute("useInvariantCulture", false))
{
this.DefaultCultureInfo = CultureInfo.InvariantCulture;
}
#pragma warning disable 618
this.ExceptionLoggingOldStyle = nlogElement.GetOptionalBooleanAttribute("exceptionLoggingOldStyle", false);
#pragma warning restore 618
bool autoReload = nlogElement.GetOptionalBooleanAttribute("autoReload", autoReloadDefault);
if (filePath != null)
this.fileMustAutoReloadLookup[GetFileLookupKey(filePath)] = autoReload;
LogManager.ThrowExceptions = nlogElement.GetOptionalBooleanAttribute("throwExceptions", LogManager.ThrowExceptions);
InternalLogger.LogToConsole = nlogElement.GetOptionalBooleanAttribute("internalLogToConsole", InternalLogger.LogToConsole);
InternalLogger.LogToConsoleError = nlogElement.GetOptionalBooleanAttribute("internalLogToConsoleError", InternalLogger.LogToConsoleError);
InternalLogger.LogFile = nlogElement.GetOptionalAttribute("internalLogFile", InternalLogger.LogFile);
InternalLogger.LogLevel = LogLevel.FromString(nlogElement.GetOptionalAttribute("internalLogLevel", InternalLogger.LogLevel.Name));
LogManager.GlobalThreshold = LogLevel.FromString(nlogElement.GetOptionalAttribute("globalThreshold", LogManager.GlobalThreshold.Name));
var children = nlogElement.Children;
//first load the extensions, as the can be used in other elements (targets etc)
var extensionsChilds = children.Where(child => child.LocalName.Equals("EXTENSIONS", StringComparison.InvariantCultureIgnoreCase));
foreach (var extensionsChild in extensionsChilds)
{
this.ParseExtensionsElement(extensionsChild, Path.GetDirectoryName(filePath));
}
//parse all other direct elements
foreach (var child in children)
{
switch (child.LocalName.ToUpper(CultureInfo.InvariantCulture))
{
case "EXTENSIONS":
//already parsed
break;
case "INCLUDE":
this.ParseIncludeElement(child, Path.GetDirectoryName(filePath), autoReloadDefault: autoReload);
break;
case "APPENDERS":
case "TARGETS":
this.ParseTargetsElement(child);
break;
case "VARIABLE":
this.ParseVariableElement(child);
break;
case "RULES":
this.ParseRulesElement(child, this.LoggingRules);
break;
case "TIME":
this.ParseTimeElement(child);
break;
default:
InternalLogger.Warn("Skipping unknown node: {0}", child.LocalName);
break;
}
}
}
示例7: ParseLoggerElement
/// <summary>
/// Parse {Logger} xml element
/// </summary>
/// <param name="loggerElement"></param>
/// <param name="rulesCollection">Rules are added to this parameter.</param>
private void ParseLoggerElement(NLogXmlElement loggerElement, IList<LoggingRule> rulesCollection)
{
loggerElement.AssertName("logger");
var namePattern = loggerElement.GetOptionalAttribute("name", "*");
var enabled = loggerElement.GetOptionalBooleanAttribute("enabled", true);
if (!enabled)
{
InternalLogger.Debug("The logger named '{0}' are disabled");
return;
}
var rule = new LoggingRule();
string appendTo = loggerElement.GetOptionalAttribute("appendTo", null);
if (appendTo == null)
{
appendTo = loggerElement.GetOptionalAttribute("writeTo", null);
}
rule.LoggerNamePattern = namePattern;
if (appendTo != null)
{
foreach (string t in appendTo.Split(','))
{
string targetName = t.Trim();
Target target = FindTargetByName(targetName);
if (target != null)
{
rule.Targets.Add(target);
}
else
{
throw new NLogConfigurationException("Target " + targetName + " not found.");
}
}
}
rule.Final = loggerElement.GetOptionalBooleanAttribute("final", false);
string levelString;
if (loggerElement.AttributeValues.TryGetValue("level", out levelString))
{
LogLevel level = LogLevel.FromString(levelString);
rule.EnableLoggingForLevel(level);
}
else if (loggerElement.AttributeValues.TryGetValue("levels", out levelString))
{
levelString = CleanSpaces(levelString);
string[] tokens = levelString.Split(',');
foreach (string s in tokens)
{
if (!string.IsNullOrEmpty(s))
{
LogLevel level = LogLevel.FromString(s);
rule.EnableLoggingForLevel(level);
}
}
}
else
{
int minLevel = 0;
int maxLevel = LogLevel.MaxLevel.Ordinal;
string minLevelString;
string maxLevelString;
if (loggerElement.AttributeValues.TryGetValue("minLevel", out minLevelString))
{
minLevel = LogLevel.FromString(minLevelString).Ordinal;
}
if (loggerElement.AttributeValues.TryGetValue("maxLevel", out maxLevelString))
{
maxLevel = LogLevel.FromString(maxLevelString).Ordinal;
}
for (int i = minLevel; i <= maxLevel; ++i)
{
rule.EnableLoggingForLevel(LogLevel.FromOrdinal(i));
}
}
foreach (var child in loggerElement.Children)
{
switch (child.LocalName.ToUpper(CultureInfo.InvariantCulture))
{
case "FILTERS":
this.ParseFilters(rule, child);
break;
case "LOGGER":
this.ParseLoggerElement(child, rule.ChildRules);
break;
//.........这里部分代码省略.........
示例8: Initialize
/// <summary>
/// Initializes the configuration.
/// </summary>
/// <param name="reader"><see cref="XmlReader"/> containing the configuration section.</param>
/// <param name="fileName">Name of the file that contains the element (to be used as a base for including other files).</param>
/// <param name="ignoreErrors">Ignore any errors during configuration.</param>
private void Initialize(XmlReader reader, string fileName, bool ignoreErrors)
{
try
{
reader.MoveToContent();
var content = new NLogXmlElement(reader);
if (fileName != null)
{
InternalLogger.Info("Configuring from an XML element in {0}...", fileName);
#if SILVERLIGHT
string key = fileName;
#else
string key = Path.GetFullPath(fileName);
#endif
this.visitedFile[key] = true;
this.originalFileName = fileName;
this.ParseTopLevel(content, Path.GetDirectoryName(fileName));
}
else
{
this.ParseTopLevel(content, null);
}
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
InternalLogger.Error("Error {0}...", exception);
if (!ignoreErrors)
{
throw new NLogConfigurationException("Exception occurred when loading configuration from " + fileName, exception);
}
}
}
示例9: ParseTopLevel
private void ParseTopLevel(NLogXmlElement content, string baseDirectory)
{
content.AssertName("nlog", "configuration");
switch (content.LocalName.ToUpper(CultureInfo.InvariantCulture))
{
case "CONFIGURATION":
this.ParseConfigurationElement(content, baseDirectory);
break;
case "NLOG":
this.ParseNLogElement(content, baseDirectory);
break;
}
}
示例10: SetItemFromElement
private bool SetItemFromElement(object o, NLogXmlElement element)
{
if (element.Value != null)
return false;
string name = element.LocalName;
PropertyInfo propInfo;
if (!PropertyHelper.TryGetPropertyInfo(o, name, out propInfo))
{
return false;
}
object item = propInfo.GetValue(o, null);
this.ConfigureObjectFromAttributes(item, element, true);
this.ConfigureObjectFromElement(item, element);
return true;
}
示例11: TryCreateLayoutInstance
private Layout TryCreateLayoutInstance(NLogXmlElement element, Type type)
{
// Check if it is a Layout
if (!typeof(Layout).IsAssignableFrom(type))
return null;
string layoutTypeName = StripOptionalNamespacePrefix(element.GetOptionalAttribute("type", null));
// Check if the 'type' attribute has been specified
if (layoutTypeName == null)
return null;
return this.ConfigurationItemFactory.Layouts.CreateInstance(this.ExpandSimpleVariables(layoutTypeName));
}
示例12: SetLayoutFromElement
private bool SetLayoutFromElement(object o, NLogXmlElement layoutElement)
{
PropertyInfo targetPropertyInfo;
string name = layoutElement.LocalName;
// if property exists
if (PropertyHelper.TryGetPropertyInfo(o, name, out targetPropertyInfo))
{
Layout layout = TryCreateLayoutInstance(layoutElement, targetPropertyInfo.PropertyType);
// and is a Layout and 'type' attribute has been specified
if (layout != null)
{
this.ConfigureObjectFromAttributes(layout, layoutElement, true);
this.ConfigureObjectFromElement(layout, layoutElement);
targetPropertyInfo.SetValue(o, layout, null);
return true;
}
}
return false;
}
示例13: CheckParsingErrors
/// <summary>
/// Checks whether any error during XML configuration parsing has occured.
/// If there are any and <c>ThrowConfigExceptions</c> or <c>ThrowExceptions</c>
/// setting is enabled - throws <c>NLogConfigurationException</c>, otherwise
/// just write an internal log at Warn level.
/// </summary>
/// <param name="rootContentElement">Root NLog configuration xml element</param>
private void CheckParsingErrors(NLogXmlElement rootContentElement)
{
var parsingErrors = rootContentElement.GetParsingErrors().ToArray();
if(parsingErrors.Any())
{
if (LogManager.ThrowConfigExceptions ?? LogManager.ThrowExceptions)
{
string exceptionMessage = string.Join(Environment.NewLine, parsingErrors);
throw new NLogConfigurationException(exceptionMessage);
}
else
{
foreach (var parsingError in parsingErrors)
{
InternalLogger.Log(LogLevel.Warn, parsingError);
}
}
}
}
示例14: Initialize
/// <summary>
/// Initializes the configuration.
/// </summary>
/// <param name="reader"><see cref="XmlReader"/> containing the configuration section.</param>
/// <param name="fileName">Name of the file that contains the element (to be used as a base for including other files).</param>
/// <param name="ignoreErrors">Ignore any errors during configuration.</param>
private void Initialize(XmlReader reader, string fileName, bool ignoreErrors)
{
try
{
InitializeSucceeded = null;
reader.MoveToContent();
var content = new NLogXmlElement(reader);
if (fileName != null)
{
this.originalFileName = fileName;
this.ParseTopLevel(content, fileName, autoReloadDefault: false);
InternalLogger.Info("Configured from an XML element in {0}...", fileName);
}
else
{
this.ParseTopLevel(content, null, autoReloadDefault: false);
}
InitializeSucceeded = true;
this.CheckParsingErrors(content);
this.CheckUnusedTargets();
}
catch (Exception exception)
{
InitializeSucceeded = false;
if (exception.MustBeRethrownImmediately())
{
throw;
}
var configurationException = new NLogConfigurationException(exception, "Exception when parsing {0}. ", fileName);
InternalLogger.Error(configurationException, "Parsing configuration from {0} failed.", fileName);
if (!ignoreErrors)
{
if (configurationException.MustBeRethrown())
{
throw configurationException;
}
}
}
}
示例15: ParseTopLevel
/// <summary>
/// Parse the root
/// </summary>
/// <param name="content"></param>
/// <param name="filePath">path to config file.</param>
/// <param name="autoReloadDefault">The default value for the autoReload option.</param>
private void ParseTopLevel(NLogXmlElement content, string filePath, bool autoReloadDefault)
{
content.AssertName("nlog", "configuration");
switch (content.LocalName.ToUpper(CultureInfo.InvariantCulture))
{
case "CONFIGURATION":
this.ParseConfigurationElement(content, filePath, autoReloadDefault);
break;
case "NLOG":
this.ParseNLogElement(content, filePath, autoReloadDefault);
break;
}
}