我需要编写Nlog的代码配置,而不是在C#上将日志发送到seq服务器的nlog.config。 我不知道该使用哪个Nlog库。我一直在尝试几种选择,但对我没有用。 下面提到了nlog.config代码:
<targets>
<target name="seq" xsi:type="BufferingWrapper" bufferSize="1000"
flushTimeout="2000">
<target xsi:type="Seq" serverUrl="http://localhost:5341" apiKey="" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="seq" />
</rules>
如果您编写所需的完整代码,我会很高兴。
答案 0 :(得分:0)
如果我不能使用XML文件,这是我的Nlog配置器
定义Logger变量:
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
在您的类的构造函数中使用它,例如:
(new NLogConfigurator()).ConfigureSeqBufferTarget();
配置的激活由类本身完成:
LogManager.Configuration = config;
这里是NLogConfigurator。我主要来自NLog API
public class NLogConfigurator
{
private readonly string _logFileNamePattern;
private readonly string _logLayoutPattern;
public NLogConfigurator(string logFileNamePattern)
{
_logFileNamePattern = logFileNamePattern;
}
public NLogConfigurator()
{
string addInName = this.GetType().Assembly.GetName().Name;
_logFileNamePattern = "${specialfolder:folder=CommonApplicationData}/Company/zenon/${processname}_" + addInName + ".log";
/* Layout of LogMessages:
* See: https://github.com/nlog/NLog/wiki/Layout-Renderers
* ${callsite} - The call site (class name, method name and source information).
* ${message} - The formatted log message.
* ${onexception} - Only outputs the inner layout when exception has been defined for log message.
*/
_logLayoutPattern = @"${callsite} ${message} ${onexception:Exception information\:${exception:format=type,message,method,StackTrace:maxInnerExceptionLevel=5:innerFormat=type,message,method,StackTrace}";
}
public void Configure()
{
// See: https://github.com/nlog/NLog/wiki/Configuration-API
// Step 1. Create configuration object
var config = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
// See http://sentinel.codeplex.com/ for a log viewer
var viewerTarget = new NLogViewerTarget();
config.AddTarget("viewer", viewerTarget);
var fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
// Step 3. Set target properties
viewerTarget.Layout = _logLayoutPattern;
viewerTarget.Address = "udp://127.0.0.1:9999";
fileTarget.FileName = _logFileNamePattern;
fileTarget.Layout = _logLayoutPattern;
// Step 4. Define rules
var rule1 = new LoggingRule("*", LogLevel.Debug, viewerTarget);
config.LoggingRules.Add(rule1);
var rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);
config.LoggingRules.Add(rule2);
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
public void ConfigureUDPTarget()
{
ConfigureUDPTarget(LogLevel.Debug);
}
public void ConfigureUDPTarget(LogLevel level, string ip = "udp://127.0.0.1:9999")
{
// See: https://github.com/nlog/NLog/wiki/Configuration-API
// Step 1. Create configuration object
var config = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
// See http://sentinel.codeplex.com/ for a log viewer
var viewerTarget = GetNLogViewerTarget(ip);
var fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
// Step 3. Set target properties
fileTarget.FileName = _logFileNamePattern;
fileTarget.Layout = _logLayoutPattern;
// Step 4. Define rules
var rule1 = new LoggingRule("*", level, viewerTarget);
config.LoggingRules.Add(rule1);
var rule2 = new LoggingRule("*", level, fileTarget);
config.LoggingRules.Add(rule2);
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
public void ConfigureSeqBufferTarget(LogLevel level,
string url = "http://localhost:5341",
string apiKey = "",
int bufferSize= 1000,
int flushTimeout= 2000)
{
// See: https://github.com/nlog/NLog/wiki/Configuration-API
// Step 1. Create configuration object
var config = new LoggingConfiguration();
var seqTarget = GetSeqTarget(url, apiKey);
var bufferWrapper = new BufferingTargetWrapper
{
Name = "seq",
BufferSize = bufferSize,
FlushTimeout = flushTimeout,
WrappedTarget = seqTarget
};
config.AddTarget(bufferWrapper);
// Step 4. Define rules
var rule1 = new LoggingRule("*", level, bufferWrapper);
config.LoggingRules.Add(rule1);
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
private Target GetNLogViewerTarget(string ip = "udp://127.0.0.1:9999")
{
return new NLogViewerTarget
{
Address = ip,
Layout = _logLayoutPattern
};
}
private Target GetSeqTarget(string url = "http://localhost:5341", string apiKey="")
{
return new SeqTarget
{
ServerUrl = url,
ApiKey = apiKey
};
}
}