Skip to content

Commit 1f88208

Browse files
authored
Add null check before logging exception (#5949)
1 parent bd1d693 commit 1f88208

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/WebJobs.Script.WebHost/Global.asax.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net;
88
using System.Threading;
99
using System.Web.Http;
10+
using Microsoft.Azure.WebJobs.Logging;
1011
using Microsoft.Azure.WebJobs.Script.Config;
1112
using Microsoft.Azure.WebJobs.Script.Diagnostics;
1213
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
@@ -24,7 +25,6 @@ protected void Application_Start()
2425
{
2526
ServicePointManager.DefaultConnectionLimit = ScriptConstants.DynamicSkuConnectionLimit;
2627
}
27-
2828
ConfigureMinimumThreads(settingsManager.IsDynamicSku);
2929
VerifyAndEnableShadowCopy(webHostSettings);
3030

@@ -78,19 +78,28 @@ protected void Application_Error(object sender, EventArgs e)
7878
EventGenerator eventGenerator = new EventGenerator();
7979

8080
// Get the exception object.
81-
Exception exc = Server.GetLastError();
81+
Exception unhandledEx = Server.GetLastError();
8282
string subscriptionId = Utility.GetSubscriptionId() ?? string.Empty;
8383
string appName = Utility.GetWebsiteUniqueSlotName() ?? string.Empty;
84+
string exStackTrace = string.Empty;
85+
string exType = string.Empty;
86+
string exMessage = string.Empty;
87+
if (unhandledEx != null)
88+
{
89+
exStackTrace = unhandledEx.StackTrace == null ? string.Empty : Sanitizer.Sanitize(unhandledEx.ToFormattedString());
90+
exType = unhandledEx.GetType().ToString();
91+
exMessage = string.IsNullOrEmpty(unhandledEx.Message) ? string.Empty : unhandledEx.Message;
92+
}
8493
eventGenerator.LogFunctionTraceEvent(TraceLevel.Error,
8594
subscriptionId,
8695
appName,
8796
string.Empty,
8897
string.Empty,
8998
"Host.Startup",
90-
exc.StackTrace,
99+
exStackTrace,
91100
"Application start up failed.",
92-
exc.GetType().ToString(),
93-
exc.Message,
101+
exType,
102+
exMessage,
94103
string.Empty,
95104
string.Empty,
96105
string.Empty);

0 commit comments

Comments
 (0)