NLOG WhenRepeatedFilter和多个目标的问题

时间:2019-06-08 12:42:46

标签: c# nlog

我正在将Sentry添加到NLOG(4.6.4)中的目标之一。因为有时我们会多次触发多个错误,所以我创建了WhenRepeatedFilter来停止重复的错误,并且只记录其中的一个并带有一个计数器。此过滤器已经可以在我的mailtarget上使用。

当我添加此过滤器时,我的哨兵目标也是如此。 mailtarget仍然有效,但是Sentry目标停止工作。我会添加错误的方式吗?

当我尝试更改目标的顺序时,我的mailtarget停止工作并且Sentry在工作。

LoggingConfiguration config = new LoggingConfiguration();

var WhenFilter = new WhenRepeatedFilter();
WhenFilter.Layout = "${message}";
WhenFilter.TimeoutSeconds = 600;
WhenFilter.Action = FilterResult.Ignore;
WhenFilter.IncludeFirst = false;
WhenFilter.FilterCountPropertyName = "filteredCount";
WhenFilter.FilterCountMessageAppendFormat = "\n\nIgnored: {0}";

//File log instellen
FileTarget fileTarget = new FileTarget();

fileTarget.FileName = Path.Combine(LogPath, "automate.log");
fileTarget.ArchiveFileName = Path.Combine(LogPath, "automate.{#####}.log");
fileTarget.ArchiveAboveSize = 512000; // 500kb
fileTarget.ArchiveNumbering = ArchiveNumberingMode.Sequence;
fileTarget.MaxArchiveFiles = 10;
fileTarget.ConcurrentWrites = true;
fileTarget.KeepFileOpen = false;

fileTarget.Layout = "${longdate} | ${level} | ${callsite} | ${event-properties:item=DebId} | ${event-properties:item=Gebruiker} | ${message}";


var BufferTarget = new BufferingTargetWrapper()
{
    BufferSize = 100,
    SlidingTimeout = false,
    FlushTimeout = 5000,
    Name = "BufferTarget",
    OverflowAction = BufferingTargetWrapperOverflowAction.Flush,
    WrappedTarget = fileTarget
};
config.AddTarget("file", BufferTarget);

LoggingRule ruleFile = new LoggingRule("*", LogLevel.Error, BufferTarget);
config.LoggingRules.Add(ruleFile);

//Mail log instellen

MailTarget mailTarget = new MailTarget();
config.AddTarget("mail", mailTarget);

mailTarget.Name = "mail";
[....]
mailTarget.AddNewLines = true;
mailTarget.Layout = "${longdate} | ${windows-identity} | ${event-properties:item=Versie} | ${level} | ${callsite} ${newline} ${event-properties:item=DebId} | ${event-properties:item=Bedrijf} | ${event-properties:item=Gebruiker} | Dubbele overgeslagen:${event-context:item=filteredCount}}${newline}${event-properties:item=Ips}${newline} ${message}${newline}${newline}Stacktrace: ${stacktrace}${newline}Callsite: ${callsite}${newline}Exception: ${exception}";
mailTarget.Subject = "NLog ${level} bij ${event-context:item=Bedrijf} (${event-context:item=DebId})";
if (GlobInfo.IsServer)
    mailTarget.Subject = "NLog Server ${level} bij ${event-context:item=Bedrijf} (${event-context:item=DebId})";

LoggingRule ruleMail = new LoggingRule("*", LogLevel.Error, new LimitingTargetWrapper(mailTarget, 100, TimeSpan.FromMinutes(30)));
ruleMail.Filters.Add(WhenFilter);
config.LoggingRules.Add(ruleMail);

//Sentry
var SentryTarget = new SentryTarget();
SentryTarget.Name = "Sentry";
[....]
SentryTarget.MinimumEventLevel = LogLevel.Error.ToString();
SentryTarget.MinimumBreadcrumbLevel = LogLevel.Debug.ToString();
SentryTarget.Options.InitializeSdk = true;
SentryTarget.Options.ShutdownTimeoutSeconds = 5;
SentryTarget.Options.AddTag("App", GlobInfo.IsServer ? "Server" : "Client");
SentryTarget.Options.AttachStacktrace = true;
SentryTarget.Options.Release = GlobInfo.UpdBuildnr.ToString();
SentryTarget.Options.Environment = GlobInfo.UpdVersie;
SentryTarget.IncludeCallSite = true;
SentryTarget.IncludeCallSiteStackTrace = true;
SentryTarget.Layout = "${message}";
SentryTarget.Options.BeforeSend = BeforeSend;

LoggingRule ruleSen = new LoggingRule("*", LogLevel.Error, new LimitingTargetWrapper(SentryTarget, 100, TimeSpan.FromMinutes(30)));
ruleSen.Filters.Add(WhenFilter);
config.LoggingRules.Add(ruleSen);

1 个答案:

答案 0 :(得分:1)

将其提取为方法:

var WhenFilter = new WhenRepeatedFilter();
WhenFilter.Layout = "${message}";
WhenFilter.TimeoutSeconds = 600;
WhenFilter.Action = FilterResult.Ignore;
WhenFilter.IncludeFirst = false;
WhenFilter.FilterCountPropertyName = "filteredCount";
WhenFilter.FilterCountMessageAppendFormat = "\n\nIgnored: {0}";
return WhenFilter;

不要在两个日志规则中重复使用相同的WhenFilter

或者将两个目标都添加到同一日志规则中。