本文整理汇总了C#中NLog.Config.LoggingRule类的典型用法代码示例。如果您正苦于以下问题:C# LoggingRule类的具体用法?C# LoggingRule怎么用?C# LoggingRule使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LoggingRule类属于NLog.Config命名空间,在下文中一共展示了LoggingRule类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitializeLogging
public static void InitializeLogging() {
var config = new LoggingConfiguration();
Target debuggerTarget = new DebuggerTarget() {
Layout = "${longdate}|${level}|${logger}|${message} ${exception:format=tostring}"
};
Target consoleTarget = new ColoredConsoleTarget() {
Layout = "${longdate}|${level}|${logger}|${message} ${exception:format=tostring}"
};
#if !DEBUG
debuggerTarget = new AsyncTargetWrapper(debuggerTarget);
consoleTarget = new AsyncTargetWrapper(consoleTarget);
#else
new AsyncTargetWrapper().Wrap(); // Placeholder for optimizing imports
#endif
config.AddTarget("debugger", debuggerTarget);
config.AddTarget("console", consoleTarget);
var debuggerRule = new LoggingRule("*", LogLevel.Trace, debuggerTarget);
config.LoggingRules.Add(debuggerRule);
var consoleRule = new LoggingRule("*", LogLevel.Trace, consoleTarget);
config.LoggingRules.Add(consoleRule);
LogManager.Configuration = config;
}
示例2: BuildLogger
public static void BuildLogger()
{
var consoleTarget = new ColoredConsoleTarget
{
Layout = "${message} ${exception:format=tostring}"
};
var debuggerTarget = new DebuggerTarget()
{
Layout = "${message} ${exception:format=tostring}"
};
var consoleRule = new LoggingRule("*", LogLevel.Debug, consoleTarget);
var debuggerRule = new LoggingRule("*", LogLevel.Debug, debuggerTarget);
var config = new LoggingConfiguration();
config.LoggingRules.Add(consoleRule);
config.LoggingRules.Add(debuggerRule);
config.AddTarget("debugger", debuggerTarget);
config.AddTarget("console", consoleTarget);
LogManager.Configuration = config;
}
示例3: ConfigureViaCode
public static void ConfigureViaCode(LogLevel consoleLogLevel, LogLevel fileLogLevel)
{
// Step 1. Create configuration object
var config = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
var consoleTarget = new ConsoleTarget();
config.AddTarget("console", consoleTarget);
var fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
// Step 3. Set target properties
consoleTarget.Layout = @"${message} ${onexception:EXCEPTION\:${exception:format=tostring}}";
fileTarget.Layout = @"${date:format=yyyy-MM-dd HH\:mm\:ss.fff} [${level:uppercase=true}] ${message} ${onexception:EXCEPTION\:${exception:format=tostring}}";
fileTarget.FileName = "C:\\temp\\logs\\SpiderOpenQuant.${date:format=yyyy-MM-dd hh}.log";
fileTarget.ConcurrentWrites = false;
fileTarget.KeepFileOpen = true;
fileTarget.OpenFileCacheTimeout = 60;
// Step 4. Define rules
var rule1 = new LoggingRule("*", consoleLogLevel, consoleTarget);
config.LoggingRules.Add(rule1);
var rule2 = new LoggingRule("*", fileLogLevel, fileTarget);
config.LoggingRules.Add(rule2);
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
示例4: CreateLogger
private static Logger CreateLogger()
{
var config = new LoggingConfiguration();
var fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
fileTarget.FileName = "${basedir}\\AsteriskLog.txt";
fileTarget.Layout = "${date:format=yyyy-MM-dd HH\\:mm\\:ss}: ${message}";
fileTarget.ArchiveFileName = "${basedir}\\archives\\log.{#####}.txt";
fileTarget.ArchiveAboveSize = 10240000;
fileTarget.ArchiveNumbering = ArchiveNumberingMode.Sequence;
var rule = new LoggingRule("*", LogLevel.Trace, fileTarget);
config.LoggingRules.Add(rule);
var errorTarget = new FileTarget();
config.AddTarget("errorfile", errorTarget);
errorTarget.FileName = "${basedir}\\Errors.txt";
errorTarget.Layout = "${date:format=yyyy-MM-dd HH\\:mm\\:ss}: ${message}";
errorTarget.ArchiveFileName = "${basedir}\\archives\\log.{#####}.txt";
errorTarget.ArchiveAboveSize = 10240000;
errorTarget.ArchiveNumbering = ArchiveNumberingMode.Sequence;
var errorsRule = new LoggingRule("*", LogLevel.Error, errorTarget);
config.LoggingRules.Add(errorsRule);
LogManager.Configuration = config;
return LogManager.GetCurrentClassLogger();
}
示例5: SetupLogging
private void SetupLogging()
{
// Step 1. Create configuration object
LoggingConfiguration configuration = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
FileTarget fileTarget = new FileTarget();
configuration.AddTarget("file", fileTarget);
NLogViewerTarget log_viewer_target = new NLogViewerTarget();
log_viewer_target.Name = "viewer";
log_viewer_target.Address = "udp://127.0.0.1:9999";
configuration.AddTarget("viewer", log_viewer_target);
// Step 3. Set target properties
fileTarget.FileName = "${basedir}/file.txt";
fileTarget.Layout = "${date} ${level} ${message}";
// Step 4. Define rules
LoggingRule rule1 = new LoggingRule("*", LogLevel.Debug, fileTarget);
configuration.LoggingRules.Add(rule1);
LoggingRule rule2 = new LoggingRule("*", LogLevel.Trace, log_viewer_target);
configuration.LoggingRules.Add(rule2);
// Step 5. Activate the configuration
LogManager.Configuration = configuration;
// Example usage
log.Debug("Application started");
}
示例6: configureLogger
private void configureLogger()
{
var config = new LoggingConfiguration();
var consoleTarget = new ColoredConsoleTarget();
config.AddTarget("console", consoleTarget);
var fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
consoleTarget.Layout = @"${date:format=HH\:MM\:ss} ${logger} ${message}";
fileTarget.FileName = "${basedir}/logs/${shortdate}.log";
fileTarget.EnableFileDelete = true;
fileTarget.DeleteOldFileOnStartup = true;
fileTarget.Layout = "${message}";
var rule1 = new LoggingRule("*", LogLevel.Trace, consoleTarget);
config.LoggingRules.Add(rule1);
var rule2 = new LoggingRule("*", LogLevel.Trace, fileTarget);
config.LoggingRules.Add(rule2);
LogManager.Configuration = config;
log = LogManager.GetLogger("SeleniumTests");
}
示例7: InitNLog
public void InitNLog()
{
// Step 1. Create configuration object
NLog.Config.LoggingConfiguration gConfig = new NLog.Config.LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
NLog.Targets.ColoredConsoleTarget consoleTarget = new NLog.Targets.ColoredConsoleTarget();
gConfig.AddTarget("console", consoleTarget);
NLog.Targets.FileTarget fileTarget = new NLog.Targets.FileTarget();
gConfig.AddTarget("file", fileTarget);
// Step 3. Set target properties
consoleTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
fileTarget.FileName = System.IO.Directory.GetCurrentDirectory() + "/${shortdate}/${logger}.log";
fileTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
UnityEngine.Debug.Log(fileTarget.FileName);
// Step 4. Define rules
NLog.Config.LoggingRule rule1 = new NLog.Config.LoggingRule("*", NLog.LogLevel.Debug, consoleTarget);
gConfig.LoggingRules.Add(rule1);
NLog.Config.LoggingRule rule2 = new NLog.Config.LoggingRule("*", NLog.LogLevel.Debug, fileTarget);
gConfig.LoggingRules.Add(rule2);
// Step 5. Activate the configuration
NLog.LogManager.Configuration = gConfig;
}
示例8: SetupLogging
private static void SetupLogging() {
// Step 1. Create configuration object
LoggingConfiguration config = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();
config.AddTarget("console", consoleTarget);
FileTarget fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
fileTarget.FileName = "${basedir}/file.txt";
// Step 3. Set target properties
consoleTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
fileTarget.Layout = "${date:format=HH\\:MM\\:ss} ${message}";
// Step 4. Define rules
LoggingRule rule = new LoggingRule("*", LogLevel.Debug, consoleTarget);
config.LoggingRules.Add(rule);
rule = new LoggingRule("*", LogLevel.Debug, fileTarget);
config.LoggingRules.Add(rule);
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
示例9: LocalLogger
static LocalLogger()
{
// Creating new Logger from current class
_logger = LogManager.GetLogger(nameof(LocalLogger));
var loggerFilePath = System.Configuration.ConfigurationManager.AppSettings["loggerFilePath"];
var loggerFileName = $"{LogFileName}.{DateTime.Now:yyy-MM-dd}.log"; //$"{Environment.CurrentDirectory}/{LogFileName}.{DateTime.Now:yyy-MM-dd}.log";
// File Target Configuration
var logFileTarget = new FileTarget
{
Name = LogFileName,
FileName = $"{loggerFilePath}/{loggerFileName}",
Layout = string.Format("{0:yyy-MM-dd hh:mm} :: [{1}] :: {2} -> {3}", DateTime.Now, "${level}", "${message}", "${exception}")
};
logFileTarget.ArchiveEvery = FileArchivePeriod.Hour;
logFileTarget.ArchiveNumbering = ArchiveNumberingMode.Rolling;
logFileTarget.ArchiveFileName = new SimpleLayout($"{LogFileName}_#####.log");
// Logging Rule Configuration
var logRule = new LoggingRule("*", LogLevel.Info, logFileTarget);
// New LoggingConfiguration Object
var logConfig = new LoggingConfiguration();
// Configuring Target and Rules to LoggingConfiguration
logConfig.AddTarget(LogFileName, logFileTarget);
logConfig.LoggingRules.Add(logRule);
// Setting new LoggingConfiguration
LogManager.Configuration = logConfig;
}
示例10: ConfigLogging
private void ConfigLogging()
{
var config = new LoggingConfiguration();
var target = new DebuggerTarget
{
Layout = @"${date:format=HH\\:MM\\:ss} ${logger} ${message}"
};
var sqlTarget = new FileTarget
{
Layout = @"${date:format=HH\\:MM\\:ss} ${logger} ${message}",
FileName = "sql.log"
};
var rule = new LoggingRule( "*", LogLevel.Trace, target );
var sqlRule = new LoggingRule( "NHibernate", LogLevel.Trace, sqlTarget );
config.AddTarget( "sql", sqlTarget );
config.AddTarget( "debugger", target );
config.LoggingRules.Add( sqlRule );
config.LoggingRules.Add( rule );
LogManager.Configuration = config;
}
示例11: UsingLoggingAspectWithNLogShouldUseNlogger
public void UsingLoggingAspectWithNLogShouldUseNlogger()
{
// arrange - setup nlog
LoggingConfiguration config = new LoggingConfiguration();
MemoryTarget memoryTarget = new MemoryTarget { Layout = @"${message}" };
config.AddTarget("memory", memoryTarget);
LoggingRule rule = new LoggingRule("*", LogLevel.Debug, memoryTarget);
config.LoggingRules.Add(rule);
LogManager.Configuration = config;
Mock<IConfigurationProvider> mock = new Mock<IConfigurationProvider>();
mock.Setup(provider => provider.ShouldLog(It.IsAny<LogAttribute>())).Returns(true);
LogAttribute.ConfigurationProvider = mock.Object;
// arrange - setup logger
LogAttribute.Logger = new NLogLogger("memory");
// act
Person person = new Person { Name = "test", Balance = 0.0d };
person.Should().NotBeNull();
// assert
memoryTarget.Logs.Count.Should().Be(9, "because we called the logging 9 times");
}
示例12: ConfigureForTargetLogging
/// <summary>
/// Configures NLog for to log to the specified target so that all messages
/// above and including the specified level are output.
/// </summary>
/// <param name="target">The target to log all messages to.</param>
/// <param name="minLevel">The minimal logging level.</param>
public static void ConfigureForTargetLogging(Target target, LogLevel minLevel)
{
LoggingConfiguration config = new LoggingConfiguration();
LoggingRule rule = new LoggingRule("*", minLevel, target);
config.LoggingRules.Add(rule);
LogManager.Configuration = config;
}
示例13: Initialize
public void Initialize()
{
// Step 1. Create configuration object
var config = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
var fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
var logentriesTarget = new Le.LeTarget();
config.AddTarget("logentries", logentriesTarget);
// Step 3. Set target properties
fileTarget.FileName = "${specialfolder:folder=LocalApplicationData}/" + GlobalConstants.Paths.LogFile;
fileTarget.Layout = layout;
logentriesTarget.Layout = layout;
// Step 4. Define rules
var rule1 = new LoggingRule("*", LogLevel.Info, fileTarget);
config.LoggingRules.Add(rule1);
var rule2 = new LoggingRule("*", LogLevel.Info, logentriesTarget);
config.LoggingRules.Add(rule2);
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
示例14: Configure
public static void Configure()
{
// Step 1. Create configuration object
var config = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
var consoleTarget = new ColoredConsoleTarget();
config.AddTarget("console", consoleTarget);
var fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
// Step 3. Set target properties
consoleTarget.Layout = @"${date:format=HH\:mm\:ss} ${logger} ${message}";
fileTarget.FileName = "${basedir}/SimplePerformanceMeter.txt";
fileTarget.Layout = "${message}";
// Step 4. Define rules
var rule1 = new LoggingRule("*", LogLevel.Debug, consoleTarget);
config.LoggingRules.Add(rule1);
var rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);
config.LoggingRules.Add(rule2);
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
示例15: LoadLoggerToLoggingConfig
public static void LoadLoggerToLoggingConfig(LoggingConfiguration logConfig, IConfiguration Configuration,string sectionString)
{
var fileLoggerSection = Configuration.GetSection(sectionString).GetChildren();
foreach (var logging in fileLoggerSection)
{
var target = new FileTarget()
{
FileName = logging["fileName"],
Name = logging["name"],
Layout = logging["layoutFormat"]
};
var minLevel = logging["minLevel"] != null ? LogLevel.FromString(logging["minLevel"]) : null;
LoggingRule rule = null;
if (minLevel != null)
{
rule = new LoggingRule(logging["namePattern"], minLevel, target);
}
else
{
rule = new LoggingRule(logging["namePattern"], target);
}
var useLevels = logging["logLevel"];
if(string.IsNullOrWhiteSpace(useLevels) == false)
{
var levels = useLevels.Split(',');
foreach (var level in levels)
{
rule.EnableLoggingForLevel(LogLevel.FromString(level));
}
}
logConfig.AddTarget(target);
logConfig.LoggingRules.Add(rule);
}
}