Skip to content

Commit

Permalink
update to MongoDB.Driver 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Dec 12, 2024
1 parent eb5b9b4 commit 483d025
Show file tree
Hide file tree
Showing 6 changed files with 651 additions and 651 deletions.
129 changes: 63 additions & 66 deletions src/NLog.Mongo/ExceptionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,89 +1,86 @@
using System;
using System.Threading;
using NLog.Common;

namespace NLog.Mongo
namespace NLog.Mongo;

internal static class ExceptionHelper
{
internal static class ExceptionHelper
{
private const string LoggedKey = "NLog.ExceptionLoggedToInternalLogger";
private const string LoggedKey = "NLog.ExceptionLoggedToInternalLogger";

/// <summary>
/// Mark this exception as logged to the <see cref="InternalLogger"/>.
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public static void MarkAsLoggedToInternalLogger(this Exception exception)
/// <summary>
/// Mark this exception as logged to the <see cref="InternalLogger"/>.
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public static void MarkAsLoggedToInternalLogger(this Exception exception)
{
if (exception != null)
{
if (exception != null)
{
exception.Data[LoggedKey] = true;
}
exception.Data[LoggedKey] = true;
}
}

/// <summary>
/// Is this exception logged to the <see cref="InternalLogger"/>?
/// </summary>
/// <param name="exception"></param>
/// <returns><c>true</c>if the <paramref name="exception"/> has been logged to the <see cref="InternalLogger"/>.</returns>
public static bool IsLoggedToInternalLogger(this Exception exception)
/// <summary>
/// Is this exception logged to the <see cref="InternalLogger"/>?
/// </summary>
/// <param name="exception"></param>
/// <returns><c>true</c>if the <paramref name="exception"/> has been logged to the <see cref="InternalLogger"/>.</returns>
public static bool IsLoggedToInternalLogger(this Exception exception)
{
if (exception != null)
{
if (exception != null)
{
return exception.Data[LoggedKey] as bool? ?? false;
}
return false;
return exception.Data[LoggedKey] as bool? ?? false;
}
return false;
}


/// <summary>
/// Determines whether the exception must be rethrown and logs the error to the <see cref="InternalLogger"/> if <see cref="IsLoggedToInternalLogger"/> is <c>false</c>.
///
/// Advised to log first the error to the <see cref="InternalLogger"/> before calling this method.
/// </summary>
/// <param name="exception">The exception to check.</param>
/// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns>
public static bool MustBeRethrown(this Exception exception)
{
if (exception.MustBeRethrownImmediately())
return true;

var isConfigError = exception is NLogConfigurationException;
/// <summary>
/// Determines whether the exception must be rethrown and logs the error to the <see cref="InternalLogger"/> if <see cref="IsLoggedToInternalLogger"/> is <c>false</c>.
///
/// Advised to log first the error to the <see cref="InternalLogger"/> before calling this method.
/// </summary>
/// <param name="exception">The exception to check.</param>
/// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns>
public static bool MustBeRethrown(this Exception exception)
{
if (exception.MustBeRethrownImmediately())
return true;

//we throw always configuration exceptions (historical)
if (!exception.IsLoggedToInternalLogger())
{
var level = isConfigError ? LogLevel.Warn : LogLevel.Error;
InternalLogger.Log(exception, level, "Error has been raised.");
}
var isConfigError = exception is NLogConfigurationException;

//if ThrowConfigExceptions == null, use ThrowExceptions
var shallRethrow = isConfigError ? (LogManager.ThrowConfigExceptions ?? LogManager.ThrowExceptions) : LogManager.ThrowExceptions;
return shallRethrow;
//we throw always configuration exceptions (historical)
if (!exception.IsLoggedToInternalLogger())
{
var level = isConfigError ? LogLevel.Warn : LogLevel.Error;
InternalLogger.Log(exception, level, "Error has been raised.");
}

/// <summary>
/// Determines whether the exception must be rethrown immediately, without logging the error to the <see cref="InternalLogger"/>.
///
/// Only used this method in special cases.
/// </summary>
/// <param name="exception">The exception to check.</param>
/// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns>
public static bool MustBeRethrownImmediately(this Exception exception)
{
//if ThrowConfigExceptions == null, use ThrowExceptions
var shallRethrow = isConfigError ? (LogManager.ThrowConfigExceptions ?? LogManager.ThrowExceptions) : LogManager.ThrowExceptions;
return shallRethrow;
}

/// <summary>
/// Determines whether the exception must be rethrown immediately, without logging the error to the <see cref="InternalLogger"/>.
///
/// Only used this method in special cases.
/// </summary>
/// <param name="exception">The exception to check.</param>
/// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns>
public static bool MustBeRethrownImmediately(this Exception exception)
{

#if !NETSTANDARD1_5
if (exception is StackOverflowException)
return true;
if (exception is StackOverflowException)
return true;

if (exception is ThreadAbortException)
return true;
if (exception is ThreadAbortException)
return true;
#endif

if (exception is OutOfMemoryException)
return true;
if (exception is OutOfMemoryException)
return true;

return false;
}
return false;
}
}
Loading

0 comments on commit 483d025

Please sign in to comment.