<NME> LogHelper.cs
<BEF> // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Linq;
using Microsoft.IdentityModel.Abstractions;

namespace Microsoft.IdentityModel.Logging
{
    /// <summary>
    /// Helper class for logging.
    /// </summary>
        /// <param name="exceptionType">Type of the exception to be thrown</param>
        /// <param name="exception">Exception parameter to be passed to the exception thrown.</param>
        /// <param name="throwException">boolean to set whether to throw exception or not. Default is true.</param>
        public static void Throw(string message, Type exceptionType, EventLevel logLevel, object exception = null, bool throwException = true)
        {
            if (logLevel == EventLevel.Error)
            {
        /// <summary>
        /// Indicates whether the log message header (contains library version, date/time, and PII debugging information) has been written.
        /// </summary>
        private static bool _isHeaderWritten = false;

        /// <summary>

            if (throwException)
            {
                throw (Exception)Activator.CreateInstance(exceptionType, exception ?? message);
            }
        }
    }
        }

        /// <summary>
        /// Logs an exception using the event source logger and returns new <see cref="ArgumentNullException"/> exception.
        /// </summary>
        /// <param name="argument">argument that is null or empty.</param>
        /// <remarks>EventLevel is set to Error.</remarks>
        public static ArgumentNullException LogArgumentNullException(string argument)
        {
            return LogArgumentException<ArgumentNullException>(EventLevel.Error, argument, "IDX10000: The parameter '{0}' cannot be a 'null' or an empty object. ", argument);
        }

        /// <summary>
        /// Logs an exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="message">message to log.</param>
        /// <remarks>EventLevel is set to Error.</remarks>
        public static T LogException<T>(string message) where T : Exception
        {
            return LogException<T>(EventLevel.Error, null, message, null);
        }

        /// <summary>
        /// Logs an argument exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>
        /// <param name="message">message to log.</param>
        /// <remarks>EventLevel is set to Error.</remarks>
        public static T LogArgumentException<T>(string argumentName, string message) where T : ArgumentException
        {
            return LogArgumentException<T>(EventLevel.Error, argumentName, null, message, null);
        }


        /// <summary>
        /// Logs an exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="format">Format string of the log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        /// <remarks>EventLevel is set to Error.</remarks>
        public static T LogException<T>(string format, params object[] args) where T : Exception
        {
            return LogException<T>(EventLevel.Error, null, format, args);
        }

        /// <summary>
        /// Logs an argument exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>
        /// <param name="format">Format string of the log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        /// <remarks>EventLevel is set to Error.</remarks>
        public static T LogArgumentException<T>(string argumentName, string format, params object[] args) where T : ArgumentException
        {
            return LogArgumentException<T>(EventLevel.Error, argumentName, null, format, args);
        }

        /// <summary>
        /// Logs an exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>
        /// <param name="message">message to log.</param>
        /// <remarks>EventLevel is set to Error.</remarks>
        public static T LogException<T>(Exception innerException, string message) where T : Exception
        {
            return LogException<T>(EventLevel.Error, innerException, message, null);
        }

        /// <summary>
        /// Logs an argument exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>
        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>
        /// <param name="message">message to log.</param>
        /// <remarks>EventLevel is set to Error.</remarks>
        public static T LogArgumentException<T>(string argumentName, Exception innerException, string message) where T : ArgumentException
        {
            return LogArgumentException<T>(EventLevel.Error, argumentName, innerException, message, null);
        }

        /// <summary>
        /// Logs an exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>
        /// <param name="format">Format string of the log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        /// <remarks>EventLevel is set to Error.</remarks>
        public static T LogException<T>(Exception innerException, string format, params object[] args) where T : Exception
        {
            return LogException<T>(EventLevel.Error, innerException, format, args);
        }

        /// <summary>
        /// Logs an argument exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>
        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>
        /// <param name="format">Format string of the log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        /// <remarks>EventLevel is set to Error.</remarks>
        public static T LogArgumentException<T>(string argumentName, Exception innerException, string format, params object[] args) where T : ArgumentException
        {
            return LogArgumentException<T>(EventLevel.Error, argumentName, innerException, format, args);
        }

        /// <summary>
        /// Logs an exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>
        /// <param name="message">message to log.</param>
        public static T LogException<T>(EventLevel eventLevel, string message) where T : Exception
        {
            return LogException<T>(eventLevel, null, message, null);
        }

        /// <summary>
        /// Logs an argument exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>
        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>
        /// <param name="message">message to log.</param>
        public static T LogArgumentException<T>(EventLevel eventLevel, string argumentName, string message) where T : ArgumentException
        {
            return LogArgumentException<T>(eventLevel, argumentName, null, message, null);
        }

        /// <summary>
        /// Logs an exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>
        /// <param name="format">Format string of the log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        public static T LogException<T>(EventLevel eventLevel, string format, params object[] args) where T : Exception
        {
            return LogException<T>(eventLevel, null, format, args);
        }

        /// <summary>
        /// Logs an argument exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>
        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>
        /// <param name="format">Format string of the log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        public static T LogArgumentException<T>(EventLevel eventLevel, string argumentName, string format, params object[] args) where T : ArgumentException
        {
            return LogArgumentException<T>(eventLevel, argumentName, null, format, args);
        }

        /// <summary>
        /// Logs an exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>
        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>
        /// <param name="message">message to log.</param>
        public static T LogException<T>(EventLevel eventLevel, Exception innerException, string message) where T : Exception
        {
            return LogException<T>(eventLevel, innerException, message, null);
        }

        /// <summary>
        /// Logs an argument exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>
        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>
        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>
        /// <param name="message">message to log.</param>
        public static T LogArgumentException<T>(EventLevel eventLevel, string argumentName, Exception innerException, string message) where T : ArgumentException
        {
            return LogArgumentException<T>(eventLevel, argumentName, innerException, message, null);
        }

        /// <summary>
        /// Logs an exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>
        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>
        /// <param name="format">Format string of the log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        public static T LogException<T>(EventLevel eventLevel, Exception innerException, string format, params object[] args) where T : Exception
        {
            return LogExceptionImpl<T>(eventLevel, null, innerException, format, args);
        }

        /// <summary>
        /// Logs an argument exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>
        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>
        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>
        /// <param name="format">Format string of the log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        public static T LogArgumentException<T>(EventLevel eventLevel, string argumentName, Exception innerException, string format, params object[] args) where T : ArgumentException
        {
            return LogExceptionImpl<T>(eventLevel, argumentName, innerException, format, args);
        }

        /// <summary>
        /// Logs an exception using the event source logger.
        /// </summary>
        /// <param name="exception">The exception to log.</param>
        public static Exception LogExceptionMessage(Exception exception)
        {
            return LogExceptionMessage(EventLevel.Error, exception);
        }

        /// <summary>
        /// Logs an exception using the event source logger.
        /// </summary>
        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>
        /// <param name="exception">The exception to log.</param>
        public static Exception LogExceptionMessage(EventLevel eventLevel, Exception exception)
        {
            if (exception == null)
                return null;

            if (IdentityModelEventSource.Logger.IsEnabled() && IdentityModelEventSource.Logger.LogLevel >= eventLevel)
                IdentityModelEventSource.Logger.Write(eventLevel, exception.InnerException, exception.Message);

            EventLogLevel eventLogLevel = Enum.IsDefined(typeof(EventLogLevel), (int)eventLevel) ? (EventLogLevel)eventLevel : EventLogLevel.Error;
            if (Logger.IsEnabled(eventLogLevel))
                Logger.Log(WriteEntry((EventLogLevel)eventLevel, exception.InnerException, exception.Message, null));

            return exception;
        }

        /// <summary>
        /// Logs an information event.
        /// </summary>
        /// <param name="message">The log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        public static void LogInformation(string message, params object[] args)
        {
            if (IdentityModelEventSource.Logger.IsEnabled() && IdentityModelEventSource.Logger.LogLevel >= EventLevel.Informational)
                IdentityModelEventSource.Logger.WriteInformation(message, args);

            if (Enum.IsDefined(typeof(EventLogLevel), (int)EventLevel.Informational) && Logger.IsEnabled((EventLogLevel)EventLevel.Informational))
                Logger.Log(WriteEntry((EventLogLevel)EventLevel.Informational, null, message, args));
        }

        /// <summary>
        /// Logs a verbose event.
        /// </summary>
        /// <param name="message">The log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        public static void LogVerbose(string message, params object[] args)
        {
            if (IdentityModelEventSource.Logger.IsEnabled())
                IdentityModelEventSource.Logger.WriteVerbose(message, args);

            if (Enum.IsDefined(typeof(EventLogLevel), (int)EventLevel.Verbose) && Logger.IsEnabled((EventLogLevel)EventLevel.Verbose))
                Logger.Log(WriteEntry((EventLogLevel)EventLevel.Verbose, null, message, args));
        }

        /// <summary>
        /// Logs a warning event.
        /// </summary>
        /// <param name="message">The log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        public static void LogWarning(string message, params object[] args)
        {
            if (IdentityModelEventSource.Logger.IsEnabled())
                IdentityModelEventSource.Logger.WriteWarning(message, args);

            if (Enum.IsDefined(typeof(EventLogLevel), (int)EventLevel.Warning) && Logger.IsEnabled((EventLogLevel)EventLevel.Warning))
                Logger.Log(WriteEntry((EventLogLevel)EventLevel.Warning, null, message, args));
        }

        /// <summary>
        /// Logs an exception using the event source logger and returns new typed exception.
        /// </summary>
        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>
        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>
        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>
        /// <param name="format">Format string of the log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        private static T LogExceptionImpl<T>(EventLevel eventLevel, string argumentName, Exception innerException, string format, params object[] args) where T : Exception 
        {
            string message = null;

            if (args != null)
                message = string.Format(CultureInfo.InvariantCulture, format, args);
            else
                message = format;

            if (IdentityModelEventSource.Logger.IsEnabled() && IdentityModelEventSource.Logger.LogLevel >= eventLevel)
                IdentityModelEventSource.Logger.Write(eventLevel, innerException, message);

            EventLogLevel eventLogLevel = Enum.IsDefined(typeof(EventLogLevel), (int)eventLevel) ? (EventLogLevel)eventLevel : EventLogLevel.Error;
            if (Logger.IsEnabled(eventLogLevel))
                Logger.Log(WriteEntry((EventLogLevel)eventLevel, innerException, message, null));

            if (innerException != null) 
                if (string.IsNullOrEmpty(argumentName))
                    return (T)Activator.CreateInstance(typeof(T), message, innerException);
                else
                    return (T)Activator.CreateInstance(typeof(T), argumentName, message, innerException);
            else
                if (string.IsNullOrEmpty(argumentName))
                    return (T)Activator.CreateInstance(typeof(T), message);
                else
                    return (T)Activator.CreateInstance(typeof(T), argumentName, message);
        }

        /// <summary>
        /// Formats the string using InvariantCulture
        /// </summary>
        /// <param name="format">Format string.</param>
        /// <param name="args">Format arguments.</param>
        /// <returns>Formatted string.</returns>
        public static string FormatInvariant(string format, params object[] args)
        {
            if (format == null)
                return string.Empty;

            if (args == null)
                return format;

            if (!IdentityModelEventSource.ShowPII)
                return string.Format(CultureInfo.InvariantCulture, format, args.Select(RemovePII).ToArray()); 

            return string.Format(CultureInfo.InvariantCulture, format, args);
        }

        private static string RemovePII(object arg)
        {
            if (arg is Exception ex && IsCustomException(ex))
                return ex.ToString();

            if (arg is NonPII)
                return arg.ToString();

            return string.Format(CultureInfo.InvariantCulture, IdentityModelEventSource.HiddenPIIString, arg?.GetType().ToString() ?? "Null");
        }

        internal static bool IsCustomException(Exception ex)
        {
            return ex.GetType().FullName.StartsWith("Microsoft.IdentityModel.", StringComparison.Ordinal);
        }

        /// <summary>
        /// Marks a log message argument (<paramref name="arg"/>) as NonPII.
        /// </summary>
        /// <param name="arg">A log message argument to be marked as NonPII.</param>
        /// <returns>An argument marked as NonPII.</returns>
        /// <remarks>
        /// Marking an argument as NonPII in <see cref="LogHelper.FormatInvariant"/> calls will result in logging
        /// that argument in cleartext, regardless of the <see cref="IdentityModelEventSource.ShowPII"/> flag value.
        /// </remarks>
        public static object MarkAsNonPII(object arg)
        {
            return new NonPII(arg);
        }

        /// <summary>
        /// Creates a <see cref="LogEntry"/> by using the provided event level, exception argument, string argument and arguments list.
        /// </summary>
        /// <param name="eventLogLevel"><see cref="EventLogLevel"/></param>
        /// <param name="innerException"><see cref="Exception"/></param>
        /// <param name="message">The log message.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        private static LogEntry WriteEntry(EventLogLevel eventLogLevel, Exception innerException, string message, params object[] args)
        {
            if (string.IsNullOrEmpty(message))
                return null;

            if (innerException != null)
            {
                // if PII is turned off and 'innerException' is a System exception only display the exception type
                if (!IdentityModelEventSource.ShowPII && !LogHelper.IsCustomException(innerException))
                    message = string.Format(CultureInfo.InvariantCulture, "Message: {0}, InnerException: {1}. ", message, innerException.GetType());
                else // otherwise it's safe to display the entire exception message
                    message = string.Format(CultureInfo.InvariantCulture, "Message: {0}, InnerException: {1}. ", message, innerException.Message);
            }

            message = args == null ? message : FormatInvariant(message, args);

            LogEntry entry = new LogEntry();
            entry.EventLogLevel = eventLogLevel;

            // Prefix header (library version, DateTime, whether PII is ON/OFF) to the first message logged by Wilson.
            if (!_isHeaderWritten)
            {
                string headerMessage = string.Format(CultureInfo.InvariantCulture, "Microsoft.IdentityModel Version: {0}. Date {1}. {2}",
                    typeof(IdentityModelEventSource).Assembly.GetName().Version.ToString(),
                    DateTime.UtcNow,
                    IdentityModelEventSource.ShowPII ? _piiOnLogMessage : _piiOffLogMessage);

                entry.Message = headerMessage + Environment.NewLine + message;

                _isHeaderWritten = true;
            }
            else
                entry.Message = message;

            return entry;
        }
    }
}

<MSG> fixing exception issue in loghelper

<DFF> @@ -15,7 +15,7 @@ namespace Microsoft.IdentityModel.Logging
         /// <param name="exceptionType">Type of the exception to be thrown</param>
         /// <param name="exception">Exception parameter to be passed to the exception thrown.</param>
         /// <param name="throwException">boolean to set whether to throw exception or not. Default is true.</param>
-        public static void Throw(string message, Type exceptionType, EventLevel logLevel, object exception = null, bool throwException = true)
+        public static void Throw(string message, Type exceptionType, EventLevel logLevel, Exception exception = null, bool throwException = true)
         {
             if (logLevel == EventLevel.Error)
             {
@@ -28,7 +28,14 @@ namespace Microsoft.IdentityModel.Logging
 
             if (throwException)
             {
-                throw (Exception)Activator.CreateInstance(exceptionType, exception ?? message);
+                if (exception != null)
+                {
+                    throw (Exception)Activator.CreateInstance(exceptionType, message, exception);
+                }
+                else
+                {
+                    throw (Exception)Activator.CreateInstance(exceptionType, message);
+                }
             }
         }
     }

{'commit': 'diff --git a/src/Microsoft.IdentityModel.Logging/LogHelper.cs b/src/Microsoft.IdentityModel.Logging/LogHelper.cs\nindex 88f5bd19..cdd53f62 100644\n--- a/src/Microsoft.IdentityModel.Logging/LogHelper.cs\n+++ b/src/Microsoft.IdentityModel.Logging/LogHelper.cs\n@@ -15,7 +15,7 @@ namespace Microsoft.IdentityModel.Logging\n         /// <param name="exceptionType">Type of the exception to be thrown</param>\n         /// <param name="exception">Exception parameter to be passed to the exception thrown.</param>\n         /// <param name="throwException">boolean to set whether to throw exception or not. Default is true.</param>\n-        public static void Throw(string message, Type exceptionType, EventLevel logLevel, object exception = null, bool throwException = true)\n+        public static void Throw(string message, Type exceptionType, EventLevel logLevel, Exception exception = null, bool throwException = true)\n         {\n             if (logLevel == EventLevel.Error)\n             {\n@@ -28,7 +28,14 @@ namespace Microsoft.IdentityModel.Logging\n \n             if (throwException)\n             {\n-                throw (Exception)Activator.CreateInstance(exceptionType, exception ?? message);\n+                if (exception != null)\n+                {\n+                    throw (Exception)Activator.CreateInstance(exceptionType, message, exception);\n+                }\n+                else\n+                {\n+                    throw (Exception)Activator.CreateInstance(exceptionType, message);\n+                }\n             }\n         }\n     }\ndiff --git a/src/System.IdentityModel.Tokens/Jwt/JwtSecurityToken.cs b/src/System.IdentityModel.Tokens/Jwt/JwtSecurityToken.cs\nindex b118520e..58fdf79d 100644\n--- a/src/System.IdentityModel.Tokens/Jwt/JwtSecurityToken.cs\n+++ b/src/System.IdentityModel.Tokens/Jwt/JwtSecurityToken.cs\n@@ -126,7 +126,7 @@ namespace System.IdentityModel.Tokens\n         {\n             if (header == null)\n             {\n-                LogHelper.Throw(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10000, GetType() + ": header"), typeof(ArgumentException), EventLevel.Verbose);\n+                LogHelper.Throw(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10000, GetType() + ": header"), typeof(ArgumentNullException), EventLevel.Verbose);\n             }\n \n             if (payload == null)\n@@ -388,7 +388,7 @@ namespace System.IdentityModel.Tokens\n                     throw;\n                 }\n \n-                LogHelper.Throw(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10703, "header", tokenParts[0], jwtEncodedString), typeof(ArgumentException), EventLevel.Error);\n+                LogHelper.Throw(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10703, "header", tokenParts[0], jwtEncodedString), typeof(ArgumentException), EventLevel.Error, ex);\n             }\n \n             try\n@@ -403,7 +403,7 @@ namespace System.IdentityModel.Tokens\n                     throw;\n                 }\n \n-                LogHelper.Throw(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10703, "payload", tokenParts[1], jwtEncodedString), typeof(ArgumentException), EventLevel.Error);\n+                LogHelper.Throw(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10703, "payload", tokenParts[1], jwtEncodedString), typeof(ArgumentException), EventLevel.Error, ex);\n             }\n \n             if (!string.IsNullOrEmpty(tokenParts[2]))\n@@ -419,7 +419,7 @@ namespace System.IdentityModel.Tokens\n                         throw;\n                     }\n \n-                    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10703, "signature", tokenParts[2], jwtEncodedString), ex);\n+                    LogHelper.Throw(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10703, "signature", tokenParts[2], jwtEncodedString), typeof(ArgumentException), EventLevel.Error, ex);\n                 }\n             }\n \n', 'commit_subject': 'fixing exception issue in loghelper', 'repo_name': 'AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet', 'committer': {'name': 'tushar gupta', 'email': '505693caf54a942005e9ed3364394bcb01a4fc52@gmail.com', 'time_sec': '1428696786', 'tz_offset': '-420', 'date': {'seconds': '1428696786'}}, 'commit_tree': 'b42ac7eca695df9ff91efe773a3d49c8c1d15b8b', 'commit_message': 'fixing exception issue in loghelper\n', 'language_name': 'HTML', 'license': 'mit', 'hunks': '@@ -15,7 +15,7 @@ namespace Microsoft.IdentityModel.Logging\n         /// <param name="exceptionType">Type of the exception to be thrown</param>\n         /// <param name="exception">Exception parameter to be passed to the exception thrown.</param>\n         /// <param name="throwException">boolean to set whether to throw exception or not. Default is true.</param>\n-        public static void Throw(string message, Type exceptionType, EventLevel logLevel, object exception = null, bool throwException = true)\n+        public static void Throw(string message, Type exceptionType, EventLevel logLevel, Exception exception = null, bool throwException = true)\n         {\n             if (logLevel == EventLevel.Error)\n             {\n@@ -28,7 +28,14 @@ namespace Microsoft.IdentityModel.Logging\n \n             if (throwException)\n             {\n-                throw (Exception)Activator.CreateInstance(exceptionType, exception ?? message);\n+                if (exception != null)\n+                {\n+                    throw (Exception)Activator.CreateInstance(exceptionType, message, exception);\n+                }\n+                else\n+                {\n+                    throw (Exception)Activator.CreateInstance(exceptionType, message);\n+                }\n             }\n         }\n     }\n', 'addition_count': 9, 'deletion_count': 2, 'src_file': 'a/src/Microsoft.IdentityModel.Logging/LogHelper.cs', 'tgt_file': 'b/src/Microsoft.IdentityModel.Logging/LogHelper.cs', 'file_extension': '.cs', 'before_file': '// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT License.\n\nusing System;\nusing System.Diagnostics.Tracing;\nusing System.Globalization;\nusing System.Linq;\nusing Microsoft.IdentityModel.Abstractions;\n\nnamespace Microsoft.IdentityModel.Logging\n{\n    /// <summary>\n    /// Helper class for logging.\n    /// </summary>\n        /// <param name="exceptionType">Type of the exception to be thrown</param>\n        /// <param name="exception">Exception parameter to be passed to the exception thrown.</param>\n        /// <param name="throwException">boolean to set whether to throw exception or not. Default is true.</param>\n        public static void Throw(string message, Type exceptionType, EventLevel logLevel, object exception = null, bool throwException = true)\n        {\n            if (logLevel == EventLevel.Error)\n            {\n        /// <summary>\n        /// Indicates whether the log message header (contains library version, date/time, and PII debugging information) has been written.\n        /// </summary>\n        private static bool _isHeaderWritten = false;\n\n        /// <summary>\n\n            if (throwException)\n            {\n                throw (Exception)Activator.CreateInstance(exceptionType, exception ?? message);\n            }\n        }\n    }\n        }\n\n        /// <summary>\n        /// Logs an exception using the event source logger and returns new <see cref="ArgumentNullException"/> exception.\n        /// </summary>\n        /// <param name="argument">argument that is null or empty.</param>\n        /// <remarks>EventLevel is set to Error.</remarks>\n        public static ArgumentNullException LogArgumentNullException(string argument)\n        {\n            return LogArgumentException<ArgumentNullException>(EventLevel.Error, argument, "IDX10000: The parameter \'{0}\' cannot be a \'null\' or an empty object. ", argument);\n        }\n\n        /// <summary>\n        /// Logs an exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="message">message to log.</param>\n        /// <remarks>EventLevel is set to Error.</remarks>\n        public static T LogException<T>(string message) where T : Exception\n        {\n            return LogException<T>(EventLevel.Error, null, message, null);\n        }\n\n        /// <summary>\n        /// Logs an argument exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>\n        /// <param name="message">message to log.</param>\n        /// <remarks>EventLevel is set to Error.</remarks>\n        public static T LogArgumentException<T>(string argumentName, string message) where T : ArgumentException\n        {\n            return LogArgumentException<T>(EventLevel.Error, argumentName, null, message, null);\n        }\n\n\n        /// <summary>\n        /// Logs an exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="format">Format string of the log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        /// <remarks>EventLevel is set to Error.</remarks>\n        public static T LogException<T>(string format, params object[] args) where T : Exception\n        {\n            return LogException<T>(EventLevel.Error, null, format, args);\n        }\n\n        /// <summary>\n        /// Logs an argument exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>\n        /// <param name="format">Format string of the log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        /// <remarks>EventLevel is set to Error.</remarks>\n        public static T LogArgumentException<T>(string argumentName, string format, params object[] args) where T : ArgumentException\n        {\n            return LogArgumentException<T>(EventLevel.Error, argumentName, null, format, args);\n        }\n\n        /// <summary>\n        /// Logs an exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>\n        /// <param name="message">message to log.</param>\n        /// <remarks>EventLevel is set to Error.</remarks>\n        public static T LogException<T>(Exception innerException, string message) where T : Exception\n        {\n            return LogException<T>(EventLevel.Error, innerException, message, null);\n        }\n\n        /// <summary>\n        /// Logs an argument exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>\n        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>\n        /// <param name="message">message to log.</param>\n        /// <remarks>EventLevel is set to Error.</remarks>\n        public static T LogArgumentException<T>(string argumentName, Exception innerException, string message) where T : ArgumentException\n        {\n            return LogArgumentException<T>(EventLevel.Error, argumentName, innerException, message, null);\n        }\n\n        /// <summary>\n        /// Logs an exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>\n        /// <param name="format">Format string of the log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        /// <remarks>EventLevel is set to Error.</remarks>\n        public static T LogException<T>(Exception innerException, string format, params object[] args) where T : Exception\n        {\n            return LogException<T>(EventLevel.Error, innerException, format, args);\n        }\n\n        /// <summary>\n        /// Logs an argument exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>\n        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>\n        /// <param name="format">Format string of the log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        /// <remarks>EventLevel is set to Error.</remarks>\n        public static T LogArgumentException<T>(string argumentName, Exception innerException, string format, params object[] args) where T : ArgumentException\n        {\n            return LogArgumentException<T>(EventLevel.Error, argumentName, innerException, format, args);\n        }\n\n        /// <summary>\n        /// Logs an exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>\n        /// <param name="message">message to log.</param>\n        public static T LogException<T>(EventLevel eventLevel, string message) where T : Exception\n        {\n            return LogException<T>(eventLevel, null, message, null);\n        }\n\n        /// <summary>\n        /// Logs an argument exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>\n        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>\n        /// <param name="message">message to log.</param>\n        public static T LogArgumentException<T>(EventLevel eventLevel, string argumentName, string message) where T : ArgumentException\n        {\n            return LogArgumentException<T>(eventLevel, argumentName, null, message, null);\n        }\n\n        /// <summary>\n        /// Logs an exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>\n        /// <param name="format">Format string of the log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        public static T LogException<T>(EventLevel eventLevel, string format, params object[] args) where T : Exception\n        {\n            return LogException<T>(eventLevel, null, format, args);\n        }\n\n        /// <summary>\n        /// Logs an argument exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>\n        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>\n        /// <param name="format">Format string of the log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        public static T LogArgumentException<T>(EventLevel eventLevel, string argumentName, string format, params object[] args) where T : ArgumentException\n        {\n            return LogArgumentException<T>(eventLevel, argumentName, null, format, args);\n        }\n\n        /// <summary>\n        /// Logs an exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>\n        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>\n        /// <param name="message">message to log.</param>\n        public static T LogException<T>(EventLevel eventLevel, Exception innerException, string message) where T : Exception\n        {\n            return LogException<T>(eventLevel, innerException, message, null);\n        }\n\n        /// <summary>\n        /// Logs an argument exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>\n        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>\n        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>\n        /// <param name="message">message to log.</param>\n        public static T LogArgumentException<T>(EventLevel eventLevel, string argumentName, Exception innerException, string message) where T : ArgumentException\n        {\n            return LogArgumentException<T>(eventLevel, argumentName, innerException, message, null);\n        }\n\n        /// <summary>\n        /// Logs an exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>\n        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>\n        /// <param name="format">Format string of the log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        public static T LogException<T>(EventLevel eventLevel, Exception innerException, string format, params object[] args) where T : Exception\n        {\n            return LogExceptionImpl<T>(eventLevel, null, innerException, format, args);\n        }\n\n        /// <summary>\n        /// Logs an argument exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>\n        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>\n        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>\n        /// <param name="format">Format string of the log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        public static T LogArgumentException<T>(EventLevel eventLevel, string argumentName, Exception innerException, string format, params object[] args) where T : ArgumentException\n        {\n            return LogExceptionImpl<T>(eventLevel, argumentName, innerException, format, args);\n        }\n\n        /// <summary>\n        /// Logs an exception using the event source logger.\n        /// </summary>\n        /// <param name="exception">The exception to log.</param>\n        public static Exception LogExceptionMessage(Exception exception)\n        {\n            return LogExceptionMessage(EventLevel.Error, exception);\n        }\n\n        /// <summary>\n        /// Logs an exception using the event source logger.\n        /// </summary>\n        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>\n        /// <param name="exception">The exception to log.</param>\n        public static Exception LogExceptionMessage(EventLevel eventLevel, Exception exception)\n        {\n            if (exception == null)\n                return null;\n\n            if (IdentityModelEventSource.Logger.IsEnabled() && IdentityModelEventSource.Logger.LogLevel >= eventLevel)\n                IdentityModelEventSource.Logger.Write(eventLevel, exception.InnerException, exception.Message);\n\n            EventLogLevel eventLogLevel = Enum.IsDefined(typeof(EventLogLevel), (int)eventLevel) ? (EventLogLevel)eventLevel : EventLogLevel.Error;\n            if (Logger.IsEnabled(eventLogLevel))\n                Logger.Log(WriteEntry((EventLogLevel)eventLevel, exception.InnerException, exception.Message, null));\n\n            return exception;\n        }\n\n        /// <summary>\n        /// Logs an information event.\n        /// </summary>\n        /// <param name="message">The log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        public static void LogInformation(string message, params object[] args)\n        {\n            if (IdentityModelEventSource.Logger.IsEnabled() && IdentityModelEventSource.Logger.LogLevel >= EventLevel.Informational)\n                IdentityModelEventSource.Logger.WriteInformation(message, args);\n\n            if (Enum.IsDefined(typeof(EventLogLevel), (int)EventLevel.Informational) && Logger.IsEnabled((EventLogLevel)EventLevel.Informational))\n                Logger.Log(WriteEntry((EventLogLevel)EventLevel.Informational, null, message, args));\n        }\n\n        /// <summary>\n        /// Logs a verbose event.\n        /// </summary>\n        /// <param name="message">The log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        public static void LogVerbose(string message, params object[] args)\n        {\n            if (IdentityModelEventSource.Logger.IsEnabled())\n                IdentityModelEventSource.Logger.WriteVerbose(message, args);\n\n            if (Enum.IsDefined(typeof(EventLogLevel), (int)EventLevel.Verbose) && Logger.IsEnabled((EventLogLevel)EventLevel.Verbose))\n                Logger.Log(WriteEntry((EventLogLevel)EventLevel.Verbose, null, message, args));\n        }\n\n        /// <summary>\n        /// Logs a warning event.\n        /// </summary>\n        /// <param name="message">The log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        public static void LogWarning(string message, params object[] args)\n        {\n            if (IdentityModelEventSource.Logger.IsEnabled())\n                IdentityModelEventSource.Logger.WriteWarning(message, args);\n\n            if (Enum.IsDefined(typeof(EventLogLevel), (int)EventLevel.Warning) && Logger.IsEnabled((EventLogLevel)EventLevel.Warning))\n                Logger.Log(WriteEntry((EventLogLevel)EventLevel.Warning, null, message, args));\n        }\n\n        /// <summary>\n        /// Logs an exception using the event source logger and returns new typed exception.\n        /// </summary>\n        /// <param name="eventLevel">Identifies the level of an event to be logged.</param>\n        /// <param name="argumentName">Identifies the argument whose value generated the ArgumentException.</param>\n        /// <param name="innerException">the inner <see cref="Exception"/> to be added to the outer exception.</param>\n        /// <param name="format">Format string of the log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        private static T LogExceptionImpl<T>(EventLevel eventLevel, string argumentName, Exception innerException, string format, params object[] args) where T : Exception \n        {\n            string message = null;\n\n            if (args != null)\n                message = string.Format(CultureInfo.InvariantCulture, format, args);\n            else\n                message = format;\n\n            if (IdentityModelEventSource.Logger.IsEnabled() && IdentityModelEventSource.Logger.LogLevel >= eventLevel)\n                IdentityModelEventSource.Logger.Write(eventLevel, innerException, message);\n\n            EventLogLevel eventLogLevel = Enum.IsDefined(typeof(EventLogLevel), (int)eventLevel) ? (EventLogLevel)eventLevel : EventLogLevel.Error;\n            if (Logger.IsEnabled(eventLogLevel))\n                Logger.Log(WriteEntry((EventLogLevel)eventLevel, innerException, message, null));\n\n            if (innerException != null) \n                if (string.IsNullOrEmpty(argumentName))\n                    return (T)Activator.CreateInstance(typeof(T), message, innerException);\n                else\n                    return (T)Activator.CreateInstance(typeof(T), argumentName, message, innerException);\n            else\n                if (string.IsNullOrEmpty(argumentName))\n                    return (T)Activator.CreateInstance(typeof(T), message);\n                else\n                    return (T)Activator.CreateInstance(typeof(T), argumentName, message);\n        }\n\n        /// <summary>\n        /// Formats the string using InvariantCulture\n        /// </summary>\n        /// <param name="format">Format string.</param>\n        /// <param name="args">Format arguments.</param>\n        /// <returns>Formatted string.</returns>\n        public static string FormatInvariant(string format, params object[] args)\n        {\n            if (format == null)\n                return string.Empty;\n\n            if (args == null)\n                return format;\n\n            if (!IdentityModelEventSource.ShowPII)\n                return string.Format(CultureInfo.InvariantCulture, format, args.Select(RemovePII).ToArray()); \n\n            return string.Format(CultureInfo.InvariantCulture, format, args);\n        }\n\n        private static string RemovePII(object arg)\n        {\n            if (arg is Exception ex && IsCustomException(ex))\n                return ex.ToString();\n\n            if (arg is NonPII)\n                return arg.ToString();\n\n            return string.Format(CultureInfo.InvariantCulture, IdentityModelEventSource.HiddenPIIString, arg?.GetType().ToString() ?? "Null");\n        }\n\n        internal static bool IsCustomException(Exception ex)\n        {\n            return ex.GetType().FullName.StartsWith("Microsoft.IdentityModel.", StringComparison.Ordinal);\n        }\n\n        /// <summary>\n        /// Marks a log message argument (<paramref name="arg"/>) as NonPII.\n        /// </summary>\n        /// <param name="arg">A log message argument to be marked as NonPII.</param>\n        /// <returns>An argument marked as NonPII.</returns>\n        /// <remarks>\n        /// Marking an argument as NonPII in <see cref="LogHelper.FormatInvariant"/> calls will result in logging\n        /// that argument in cleartext, regardless of the <see cref="IdentityModelEventSource.ShowPII"/> flag value.\n        /// </remarks>\n        public static object MarkAsNonPII(object arg)\n        {\n            return new NonPII(arg);\n        }\n\n        /// <summary>\n        /// Creates a <see cref="LogEntry"/> by using the provided event level, exception argument, string argument and arguments list.\n        /// </summary>\n        /// <param name="eventLogLevel"><see cref="EventLogLevel"/></param>\n        /// <param name="innerException"><see cref="Exception"/></param>\n        /// <param name="message">The log message.</param>\n        /// <param name="args">An object array that contains zero or more objects to format.</param>\n        private static LogEntry WriteEntry(EventLogLevel eventLogLevel, Exception innerException, string message, params object[] args)\n        {\n            if (string.IsNullOrEmpty(message))\n                return null;\n\n            if (innerException != null)\n            {\n                // if PII is turned off and \'innerException\' is a System exception only display the exception type\n                if (!IdentityModelEventSource.ShowPII && !LogHelper.IsCustomException(innerException))\n                    message = string.Format(CultureInfo.InvariantCulture, "Message: {0}, InnerException: {1}. ", message, innerException.GetType());\n                else // otherwise it\'s safe to display the entire exception message\n                    message = string.Format(CultureInfo.InvariantCulture, "Message: {0}, InnerException: {1}. ", message, innerException.Message);\n            }\n\n            message = args == null ? message : FormatInvariant(message, args);\n\n            LogEntry entry = new LogEntry();\n            entry.EventLogLevel = eventLogLevel;\n\n            // Prefix header (library version, DateTime, whether PII is ON/OFF) to the first message logged by Wilson.\n            if (!_isHeaderWritten)\n            {\n                string headerMessage = string.Format(CultureInfo.InvariantCulture, "Microsoft.IdentityModel Version: {0}. Date {1}. {2}",\n                    typeof(IdentityModelEventSource).Assembly.GetName().Version.ToString(),\n                    DateTime.UtcNow,\n                    IdentityModelEventSource.ShowPII ? _piiOnLogMessage : _piiOffLogMessage);\n\n                entry.Message = headerMessage + Environment.NewLine + message;\n\n                _isHeaderWritten = true;\n            }\n            else\n                entry.Message = message;\n\n            return entry;\n        }\n    }\n}\n', 'message': 'fixing exception issue in loghelper\n', 'author_name': 'tushar gupta', 'lang': 'IdentityModel'}