Skip to content

Commit bd1d693

Browse files
authored
Log application start up errors (#5933)
1 parent d7f60c2 commit bd1d693

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics;
56
using System.Linq;
67
using System.Net;
78
using System.Threading;
@@ -74,7 +75,25 @@ internal static string GetShadowCopyPath(string currentShadowCopyDirectories, st
7475

7576
protected void Application_Error(object sender, EventArgs e)
7677
{
77-
// TODO: Log any unhandled exceptions
78+
EventGenerator eventGenerator = new EventGenerator();
79+
80+
// Get the exception object.
81+
Exception exc = Server.GetLastError();
82+
string subscriptionId = Utility.GetSubscriptionId() ?? string.Empty;
83+
string appName = Utility.GetWebsiteUniqueSlotName() ?? string.Empty;
84+
eventGenerator.LogFunctionTraceEvent(TraceLevel.Error,
85+
subscriptionId,
86+
appName,
87+
string.Empty,
88+
string.Empty,
89+
"Host.Startup",
90+
exc.StackTrace,
91+
"Application start up failed.",
92+
exc.GetType().ToString(),
93+
exc.Message,
94+
string.Empty,
95+
string.Empty,
96+
string.Empty);
7897
}
7998

8099
protected void Application_End(object sender, EventArgs e)

src/WebJobs.Script/Config/ScriptSettingsManager.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,7 @@ public virtual string AzureWebsiteUniqueSlotName
4545
{
4646
get
4747
{
48-
string name = GetSetting(EnvironmentSettingNames.AzureWebsiteName);
49-
string slotName = GetSetting(EnvironmentSettingNames.AzureWebsiteSlotName);
50-
51-
if (!string.IsNullOrEmpty(slotName) &&
52-
!string.Equals(slotName, ScriptConstants.DefaultProductionSlotName, StringComparison.OrdinalIgnoreCase))
53-
{
54-
name += $"-{slotName}";
55-
}
56-
57-
return name?.ToLowerInvariant();
48+
return Utility.GetWebsiteUniqueSlotName();
5849
}
5950
}
6051

@@ -77,12 +68,7 @@ public virtual string ApplicationInsightsInstrumentationKey
7768

7869
public virtual string GetSetting(string settingKey)
7970
{
80-
if (string.IsNullOrEmpty(settingKey))
81-
{
82-
throw new ArgumentNullException(nameof(settingKey));
83-
}
84-
85-
return Environment.GetEnvironmentVariable(settingKey);
71+
return Utility.GetSetting(settingKey);
8672
}
8773

8874
public string GetSettingOrDefault(string settingKey, string defaultValue)

src/WebJobs.Script/Utility.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,30 @@ public static string GetSubscriptionId()
180180
return null;
181181
}
182182

183+
public static string GetWebsiteUniqueSlotName()
184+
{
185+
string name = GetSetting(EnvironmentSettingNames.AzureWebsiteName);
186+
string slotName = GetSetting(EnvironmentSettingNames.AzureWebsiteSlotName);
187+
188+
if (!string.IsNullOrEmpty(slotName) &&
189+
!string.Equals(slotName, ScriptConstants.DefaultProductionSlotName, StringComparison.OrdinalIgnoreCase))
190+
{
191+
name += $"-{slotName}";
192+
}
193+
194+
return name?.ToLowerInvariant();
195+
}
196+
197+
public static string GetSetting(string settingKey)
198+
{
199+
if (string.IsNullOrEmpty(settingKey))
200+
{
201+
throw new ArgumentNullException(nameof(settingKey));
202+
}
203+
204+
return Environment.GetEnvironmentVariable(settingKey);
205+
}
206+
183207
public static bool IsValidUserType(Type type)
184208
{
185209
return !type.IsInterface && !type.IsPrimitive && !(type.Namespace == "System");

0 commit comments

Comments
 (0)