本文整理汇总了C#中NLog.Config.ConfigurationItemFactory类的典型用法代码示例。如果您正苦于以下问题:C# ConfigurationItemFactory类的具体用法?C# ConfigurationItemFactory怎么用?C# ConfigurationItemFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConfigurationItemFactory类属于NLog.Config命名空间,在下文中一共展示了ConfigurationItemFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LayoutRendererThrows
public void LayoutRendererThrows()
{
ConfigurationItemFactory configurationItemFactory = new ConfigurationItemFactory();
configurationItemFactory.LayoutRenderers.RegisterDefinition("throwsException", typeof(ThrowsExceptionRenderer));
SimpleLayout l = new SimpleLayout("xx${throwsException}yy", configurationItemFactory);
string output = l.Render(LogEventInfo.CreateNullEvent());
Assert.AreEqual("xxyy", output);
}
示例2: SetPropertyFromString
internal static void SetPropertyFromString(object o, string name, string value, ConfigurationItemFactory configurationItemFactory)
{
InternalLogger.Debug("Setting '{0}.{1}' to '{2}'", o.GetType().Name, name, value);
PropertyInfo propInfo;
if (!TryGetPropertyInfo(o, name, out propInfo))
{
throw new NotSupportedException("Parameter " + name + " not supported on " + o.GetType().Name);
}
try
{
if (propInfo.IsDefined(typeof(ArrayParameterAttribute), false))
{
throw new NotSupportedException("Parameter " + name + " of " + o.GetType().Name + " is an array and cannot be assigned a scalar value.");
}
object newValue;
Type propertyType = propInfo.PropertyType;
propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;
if (!TryNLogSpecificConversion(propertyType, value, out newValue, configurationItemFactory))
{
if (!TryGetEnumValue(propertyType, value, out newValue))
{
if (!TryImplicitConversion(propertyType, value, out newValue))
{
if (!TrySpecialConversion(propertyType, value, out newValue))
{
newValue = Convert.ChangeType(value, propertyType, CultureInfo.InvariantCulture);
}
}
}
}
propInfo.SetValue(o, newValue, null);
}
catch (TargetInvocationException ex)
{
throw new NLogConfigurationException("Error when setting property '" + propInfo.Name + "' on " + o, ex.InnerException);
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
throw new NLogConfigurationException("Error when setting property '" + propInfo.Name + "' on " + o, exception);
}
}
示例3: CompileLayout
internal static LayoutRenderer[] CompileLayout(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr, bool isNested, out string text)
{
var result = new List<LayoutRenderer>();
var literalBuf = new StringBuilder();
int ch;
int p0 = sr.Position;
while ((ch = sr.Peek()) != -1)
{
if (isNested && (ch == '}' || ch == ':'))
{
break;
}
sr.Read();
if (ch == '$' && sr.Peek() == '{')
{
if (literalBuf.Length > 0)
{
result.Add(new LiteralLayoutRenderer(literalBuf.ToString()));
literalBuf.Length = 0;
}
LayoutRenderer newLayoutRenderer = ParseLayoutRenderer(configurationItemFactory, sr);
if (CanBeConvertedToLiteral(newLayoutRenderer))
{
newLayoutRenderer = ConvertToLiteral(newLayoutRenderer);
}
// layout renderer
result.Add(newLayoutRenderer);
}
else
{
literalBuf.Append((char)ch);
}
}
if (literalBuf.Length > 0)
{
result.Add(new LiteralLayoutRenderer(literalBuf.ToString()));
literalBuf.Length = 0;
}
int p1 = sr.Position;
MergeLiterals(result);
text = sr.Substring(p0, p1);
return result.ToArray();
}
示例4: Register
/// <summary>
/// Registers the Rebus correlation ID renderer under the <see cref="ItemName"/> key in the given <see cref="ConfigurationItemFactory"/>
/// </summary>
public static void Register(ConfigurationItemFactory configurationItemFactory)
{
var namedItemFactory = configurationItemFactory.LayoutRenderers;
Type dummy;
var layoutRendererHasAlreadyBeenBeenRegistered = namedItemFactory
.TryGetDefinition(ItemName, out dummy);
if (layoutRendererHasAlreadyBeenBeenRegistered)
{
return;
}
namedItemFactory.RegisterDefinition(ItemName, typeof (RebusCorrelationIdLayoutRenderer));
}
示例5: ParseExpression
/// <summary>
/// Parses the specified condition string and turns it into
/// <see cref="ConditionExpression"/> tree.
/// </summary>
/// <param name="expressionText">The expression to be parsed.</param>
/// <param name="configurationItemFactories">Instance of <see cref="ConfigurationItemFactory"/> used to resolve references to condition methods and layout renderers.</param>
/// <returns>The root of the expression syntax tree which can be used to get the value of the condition in a specified context.</returns>
public static ConditionExpression ParseExpression(string expressionText, ConfigurationItemFactory configurationItemFactories)
{
if (expressionText == null)
{
return null;
}
var parser = new ConditionParser(new SimpleStringReader(expressionText), configurationItemFactories);
ConditionExpression expression = parser.ParseExpression();
if (!parser.tokenizer.IsEOF())
{
throw new ConditionParseException("Unexpected token: " + parser.tokenizer.TokenValue);
}
return expression;
}
示例6: LayoutRendererThrows2
public void LayoutRendererThrows2()
{
string internalLogOutput = RunAndCaptureInternalLog(
() =>
{
ConfigurationItemFactory configurationItemFactory = new ConfigurationItemFactory();
configurationItemFactory.LayoutRenderers.RegisterDefinition("throwsException", typeof(ThrowsExceptionRenderer));
SimpleLayout l = new SimpleLayout("xx${throwsException:msg1}yy${throwsException:msg2}zz", new LoggingConfiguration(configurationItemFactory));
l.Initialize(CommonCfg);
string output = l.Render(LogEventInfo.CreateNullEvent());
Assert.AreEqual("xxyyzz", output);
},
LogLevel.Warn);
Assert.IsTrue(internalLogOutput.IndexOf("msg1") >= 0, internalLogOutput);
Assert.IsTrue(internalLogOutput.IndexOf("msg2") >= 0, internalLogOutput);
}
示例7: SimpleLayout
/// <summary>
/// Initializes a new instance of the <see cref="SimpleLayout"/> class.
/// </summary>
/// <param name="txt">The layout string to parse.</param>
/// <param name="configurationItemFactory">The NLog factories to use when creating references to layout renderers.</param>
public SimpleLayout(string txt, ConfigurationItemFactory configurationItemFactory)
{
this.configurationItemFactory = configurationItemFactory;
this.Text = txt;
}
示例8: CompileLayout
internal static LayoutRenderer[] CompileLayout(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr, bool isNested, out string text)
{
var result = new List<LayoutRenderer>();
var literalBuf = new StringBuilder();
int ch;
int p0 = sr.Position;
while ((ch = sr.Peek()) != -1)
{
if (isNested)
{
//escape char? Then allow }, : and \
if (ch == '\\')
{
sr.Read();
var nextChar = sr.Peek();
//char that can be escaped.
if (nextChar == '}' || nextChar == ':' || nextChar == '\\')
{
//read next char and append
sr.Read();
literalBuf.Append((char)nextChar);
}
else
{
//dont read next char and just append the slash
literalBuf.Append('\\');
}
continue;
}
if (ch == '}' || ch == ':')
{
break;
}
}
sr.Read();
if (ch == '$' && sr.Peek() == '{')
{
if (literalBuf.Length > 0)
{
result.Add(new LiteralLayoutRenderer(literalBuf.ToString()));
literalBuf.Length = 0;
}
LayoutRenderer newLayoutRenderer = ParseLayoutRenderer(configurationItemFactory, sr);
if (CanBeConvertedToLiteral(newLayoutRenderer))
{
newLayoutRenderer = ConvertToLiteral(newLayoutRenderer);
}
// layout renderer
result.Add(newLayoutRenderer);
}
else
{
literalBuf.Append((char)ch);
}
}
if (literalBuf.Length > 0)
{
result.Add(new LiteralLayoutRenderer(literalBuf.ToString()));
literalBuf.Length = 0;
}
int p1 = sr.Position;
MergeLiterals(result);
text = sr.Substring(p0, p1);
return result.ToArray();
}
示例9: CompileLayout
internal static LayoutRenderer[] CompileLayout(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr, bool isNested, out string text)
{
var result = new List<LayoutRenderer>();
var literalBuf = new StringBuilder();
int ch;
int p0 = sr.Position;
while ((ch = sr.Peek()) != -1)
{
if (isNested)
{
//possible escape char `\`
if (ch == '\\')
{
sr.Read();
var nextChar = sr.Peek();
//escape chars
if (nextChar == '}' || nextChar == ':')
{
//read next char and append
sr.Read();
literalBuf.Append((char)nextChar);
}
else
{
//dont treat \ as escape char and just read it
literalBuf.Append('\\');
}
continue;
}
if (ch == '}' || ch == ':')
{
//end of innerlayout.
// `}` is when double nested inner layout.
// `:` when single nested layout
break;
}
}
sr.Read();
//detect `${` (new layout-renderer)
if (ch == '$' && sr.Peek() == '{')
{
//stach already found layout-renderer.
if (literalBuf.Length > 0)
{
result.Add(new LiteralLayoutRenderer(literalBuf.ToString()));
literalBuf.Length = 0;
}
LayoutRenderer newLayoutRenderer = ParseLayoutRenderer(configurationItemFactory, sr);
if (CanBeConvertedToLiteral(newLayoutRenderer))
{
newLayoutRenderer = ConvertToLiteral(newLayoutRenderer);
}
// layout renderer
result.Add(newLayoutRenderer);
}
else
{
literalBuf.Append((char)ch);
}
}
if (literalBuf.Length > 0)
{
result.Add(new LiteralLayoutRenderer(literalBuf.ToString()));
literalBuf.Length = 0;
}
int p1 = sr.Position;
MergeLiterals(result);
text = sr.Substring(p0, p1);
return result.ToArray();
}
示例10: BuildDefaultFactory
/// <summary>
/// Builds the default configuration item factory.
/// </summary>
/// <returns>Default factory.</returns>
private static ConfigurationItemFactory BuildDefaultFactory()
{
var nlogAssembly = typeof(ILogger).Assembly;
var factory = new ConfigurationItemFactory(nlogAssembly);
factory.RegisterExtendedItems();
#if !SILVERLIGHT
var assemblyLocation = Path.GetDirectoryName(nlogAssembly.Location);
if (assemblyLocation == null)
{
return factory;
}
var extensionDlls = Directory.GetFiles(assemblyLocation, "NLog*.dll")
.Select(Path.GetFileName)
.Where(x => !x.Equals("NLog.dll", StringComparison.OrdinalIgnoreCase))
.Where(x => !x.Equals("NLog.UnitTests.dll", StringComparison.OrdinalIgnoreCase))
.Where(x => !x.Equals("NLog.Extended.dll", StringComparison.OrdinalIgnoreCase))
.Select(x => Path.Combine(assemblyLocation, x));
foreach (var extensionDll in extensionDlls)
{
InternalLogger.Info("Auto loading assembly file: {0}", extensionDll);
var extensionAssembly = Assembly.LoadFrom(extensionDll);
factory.RegisterItemsFromAssembly(extensionAssembly);
}
#endif
return factory;
}
示例11: BuildDefaultFactory
/// <summary>
/// Builds the default configuration item factory.
/// </summary>
/// <returns>Default factory.</returns>
private static ConfigurationItemFactory BuildDefaultFactory()
{
var nlogAssembly = typeof(ILogger).Assembly;
var factory = new ConfigurationItemFactory(nlogAssembly);
factory.RegisterExtendedItems();
#if !SILVERLIGHT
var assemblyLocation = Path.GetDirectoryName(new Uri(nlogAssembly.CodeBase).LocalPath);
if (assemblyLocation == null)
{
InternalLogger.Warn("No auto loading because Nlog.dll location is unknown");
return factory;
}
if (!Directory.Exists(assemblyLocation))
{
InternalLogger.Warn("No auto loading because '{0}' doesn't exists", assemblyLocation);
return factory;
}
try
{
var extensionDlls = Directory.GetFiles(assemblyLocation, "NLog*.dll")
.Select(Path.GetFileName)
.Where(x => !x.Equals("NLog.dll", StringComparison.OrdinalIgnoreCase))
.Where(x => !x.Equals("NLog.UnitTests.dll", StringComparison.OrdinalIgnoreCase))
.Where(x => !x.Equals("NLog.Extended.dll", StringComparison.OrdinalIgnoreCase))
.Select(x => Path.Combine(assemblyLocation, x));
InternalLogger.Debug("Start auto loading, location: {0}", assemblyLocation);
foreach (var extensionDll in extensionDlls)
{
InternalLogger.Info("Auto loading assembly file: {0}", extensionDll);
var success = false;
try
{
var extensionAssembly = Assembly.LoadFrom(extensionDll);
InternalLogger.LogAssemblyVersion(extensionAssembly);
factory.RegisterItemsFromAssembly(extensionAssembly);
success = true;
}
catch (Exception ex)
{
if (ex.MustBeRethrownImmediately())
{
throw;
}
InternalLogger.Warn(ex, "Auto loading assembly file: {0} failed! Skipping this file.", extensionDll);
//TODO NLog 5, check MustBeRethrown()
}
if (success)
{
InternalLogger.Info("Auto loading assembly file: {0} succeeded!", extensionDll);
}
}
}
catch (UnauthorizedAccessException ex)
{
InternalLogger.Warn(ex, "Seems that we do not have permission");
if (ex.MustBeRethrown())
{
throw;
}
}
InternalLogger.Debug("Auto loading done");
#endif
return factory;
}
示例12: InitNLogConfigurationItemFactory
static void InitNLogConfigurationItemFactory()
{
// Default initialization code for ConfigurationItemFactory.Default spends
// almost 0.5 sec in il-packed executable. (it scans whole types in assembly to find plugin types)
// To avoid this slow-down, manual initialization is written.
// If you need another layout-renderer, filter or anything else in NLog assembly,
// please insert register code here.
var factory = new ConfigurationItemFactory(new Assembly[0]);
factory.LayoutRenderers.RegisterDefinition("longdate", typeof(LongDateLayoutRenderer));
factory.LayoutRenderers.RegisterDefinition("level", typeof(LevelLayoutRenderer));
factory.LayoutRenderers.RegisterDefinition("logger", typeof(LoggerNameLayoutRenderer));
factory.LayoutRenderers.RegisterDefinition("message", typeof(MessageLayoutRenderer));
factory.LayoutRenderers.RegisterDefinition("exception", typeof(ExceptionLayoutRenderer));
factory.LayoutRenderers.RegisterDefinition("uppercase", typeof(UppercaseLayoutRendererWrapper));
ConfigurationItemFactory.Default = factory;
}
示例13: CustomAgnosticTests
public void CustomAgnosticTests()
{
var cif = new ConfigurationItemFactory();
cif.RegisterType(typeof(CustomRendererAgnostic), string.Empty);
Layout l = new SimpleLayout("${customAgnostic}", cif);
l.Initialize(null);
Assert.True(l.IsThreadAgnostic);
}
示例14: ConfigurationItemFactory
/// <summary>
/// Initializes static members of the <see cref="ConfigurationItemFactory"/> class.
/// </summary>
static ConfigurationItemFactory()
{
Default = new ConfigurationItemFactory(typeof(Logger).Assembly);
}
示例15: ConditionParser
/// <summary>
/// Initializes a new instance of the <see cref="ConditionParser"/> class.
/// </summary>
/// <param name="stringReader">The string reader.</param>
/// <param name="configurationItemFactory">Instance of <see cref="ConfigurationItemFactory"/> used to resolve references to condition methods and layout renderers.</param>
private ConditionParser(SimpleStringReader stringReader, ConfigurationItemFactory configurationItemFactory)
{
this.configurationItemFactory = configurationItemFactory;
this.tokenizer = new ConditionTokenizer(stringReader);
}