This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unifying logging of exceptions. Fixes #59.
- Loading branch information
Showing
5 changed files
with
100 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Microsoft.Azure.WebJobs.Extensions.EventHubs/Utility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.Azure.EventHubs; | ||
using Microsoft.Azure.EventHubs.Processor; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.Azure.WebJobs.EventHubs | ||
{ | ||
internal class Utility | ||
{ | ||
public static void LogException(Exception ex, string message, ILogger logger) | ||
{ | ||
try | ||
{ | ||
// Sometimes EventHub SDK agrigates an exception | ||
AggregateException ae = ex as AggregateException; | ||
if (ae != null && ae.InnerExceptions != null && ae.InnerExceptions.Count == 1) | ||
{ | ||
ex = ae.InnerExceptions[0]; | ||
} | ||
|
||
LogLevel logLevel = GetLevel(ex); | ||
if (logLevel == LogLevel.Information) | ||
{ | ||
message = $"{message} An exception of type '{ex.GetType().Name}' was thrown. This exception type is typically a result of Event Hub processor rebalancing or a transient error and can be safely ignored."; | ||
} | ||
logger?.Log(logLevel, 0, message, ex, (s, exc) => message); | ||
} | ||
catch | ||
{ | ||
// best effort logging | ||
} | ||
} | ||
|
||
private static LogLevel GetLevel(Exception ex) | ||
{ | ||
if (ex is ReceiverDisconnectedException || ex is LeaseLostException | ||
|| (ex is WindowsAzure.Storage.StorageException && ex.Message == "The lease ID specified did not match the lease ID for the blob.")) | ||
{ | ||
// For EventProcessorHost these exceptions can happen as part | ||
// of normal partition balancing across instances, so we want to | ||
// trace them, but not treat them as errors. | ||
return LogLevel.Information; | ||
} | ||
|
||
var ehex = ex as EventHubsException; | ||
if (!(ex is OperationCanceledException) && (ehex == null || !ehex.IsTransient)) | ||
{ | ||
// any non-transient exceptions or unknown exception types | ||
// we want to log as errors | ||
return LogLevel.Error; | ||
} | ||
else | ||
{ | ||
// transient messaging errors we log as info so we have a record | ||
// of them, but we don't treat them as actual errors | ||
return LogLevel.Information; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters