Skip to content

Commit d12db4f

Browse files
committed
Added broader exception catching to the dispatcher to tighten up the gasket keeping exceptions from leaking out.
1 parent 693d6ff commit d12db4f

1 file changed

Lines changed: 36 additions & 16 deletions

File tree

Take2/NuLog/Dispatchers/StandardDispatcher.cs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,48 @@ public StandardDispatcher(IEnumerable<ITarget> targets, ITagRouter tagRouter, IF
6767

6868
public void DispatchNow(ILogEvent logEvent)
6969
{
70-
// Ask our tag router which targets we send to
71-
var targetNames = tagRouter.Route(logEvent.Tags);
72-
73-
// For each target to dispatch to, do it
74-
foreach (var targetName in targetNames)
70+
try
7571
{
76-
// Try to find the target by name
77-
var target = targets.FirstOrDefault(t => string.Equals(targetName, t.Name, StringComparison.OrdinalIgnoreCase));
78-
if (target != null)
72+
// Ask our tag router which targets we send to
73+
var targetNames = tagRouter.Route(logEvent.Tags);
74+
75+
// For each target to dispatch to, do it
76+
foreach (var targetName in targetNames)
7977
{
80-
try
81-
{
82-
// We found it, tell the log event to write itself to the target
83-
logEvent.WriteTo(target);
84-
}
85-
catch (Exception cause)
78+
// Try to find the target by name
79+
var target = targets.FirstOrDefault(t => string.Equals(targetName, t.Name, StringComparison.OrdinalIgnoreCase));
80+
if (target != null)
8681
{
87-
// There was a problem writing to the target, report the error
88-
FallbackLog(cause, target, logEvent);
82+
try
83+
{
84+
// We found it, tell the log event to write itself to the target
85+
logEvent.WriteTo(target);
86+
}
87+
catch (Exception cause)
88+
{
89+
// There was a problem writing to the target, report the error
90+
FallbackLog(cause, target, logEvent);
91+
}
8992
}
9093
}
9194
}
95+
catch (Exception cause)
96+
{
97+
// A general failure, likely in the router, or in finding the target
98+
FallbackLog("Failure dispatching log event: {0}", cause);
99+
}
100+
}
101+
102+
private void FallbackLog(string message, params object[] args)
103+
{
104+
try
105+
{
106+
this.fallbackLogger.Log(message, args);
107+
}
108+
catch (Exception cause)
109+
{
110+
Trace.TraceError("Failure writing message to fallback logger for cause: {0}", cause);
111+
}
92112
}
93113

94114
private void FallbackLog(Exception exception, ITarget target, ILogEvent logEvent)

0 commit comments

Comments
 (0)