Skip to content

Commit 4d70917

Browse files
committed
Reduce exception dump in console
1 parent 9b8900f commit 4d70917

7 files changed

Lines changed: 29 additions & 18 deletions

File tree

src/Libraries/SmartStore.Core/Logging/log4net/Log4netLogger.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,14 @@ public void Log(LogLevel level, Exception exception, string message, object[] ar
7575
protected internal void TryAddExtendedThreadInfo(LoggingEvent loggingEvent)
7676
{
7777
HttpRequest httpRequest = null;
78+
bool httpRequestReady = HttpContext.Current?.Handler != null;
7879

7980
try
8081
{
81-
httpRequest = HttpContext.Current.Request;
82+
if (httpRequestReady)
83+
{
84+
httpRequest = HttpContext.Current.Request;
85+
}
8286
}
8387
catch
8488
{
@@ -104,10 +108,9 @@ protected internal void TryAddExtendedThreadInfo(LoggingEvent loggingEvent)
104108
{
105109
var container = EngineContext.Current.ContainerManager;
106110

107-
IWorkContext workContext;
108111

109112
// CustomerId
110-
if (container.TryResolve<IWorkContext>(null, out workContext))
113+
if (container.TryResolve<IWorkContext>(null, out IWorkContext workContext))
111114
{
112115
try
113116
{
@@ -119,10 +122,9 @@ protected internal void TryAddExtendedThreadInfo(LoggingEvent loggingEvent)
119122
}
120123
}
121124

122-
IWebHelper webHelper;
123125

124126
// Url & stuff
125-
if (container.TryResolve<IWebHelper>(null, out webHelper))
127+
if (container.TryResolve<IWebHelper>(null, out IWebHelper webHelper))
126128
{
127129
try
128130
{

src/Libraries/SmartStore.Core/WebHelper.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,9 @@ public virtual string ServerVariables(string name)
182182

183183
try
184184
{
185-
if (_httpContext != null && _httpContext.Request != null)
185+
if (_httpContext.Handler != null && _httpContext?.Request?.ServerVariables[name] != null)
186186
{
187-
if (_httpContext.Request.ServerVariables[name] != null)
188-
{
189-
result = _httpContext.Request.ServerVariables[name];
190-
}
187+
result = _httpContext.Request.ServerVariables[name];
191188
}
192189
}
193190
catch

src/Libraries/SmartStore.Services/Cms/LinkResolver.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,16 @@ protected virtual LinkResolverData Parse(string linkExpression)
228228
case LinkType.Category:
229229
case LinkType.Manufacturer:
230230
case LinkType.Topic:
231-
var id = path.ToInt();
232-
result.Value = id != 0 ? (object)id : path;
231+
if (int.TryParse(path, out var id))
232+
{
233+
// Reduce thrown exceptions in console
234+
result.Value = id;
235+
}
236+
else
237+
{
238+
result.Value = path;
239+
}
240+
233241
result.QueryString = query;
234242
break;
235243
case LinkType.Url:

src/Presentation/SmartStore.Web.Framework/DependencyRegistrar.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,15 +724,15 @@ static HttpContextBase HttpContextBaseFactory(IComponentContext ctx)
724724

725725
static bool IsRequestValid()
726726
{
727-
if (HttpContext.Current == null)
727+
if (HttpContext.Current?.Handler == null)
728728
return false;
729729

730730
try
731731
{
732732
// The "Request" property throws at application startup on IIS integrated pipeline mode
733733
var req = HttpContext.Current.Request;
734734
}
735-
catch (Exception)
735+
catch
736736
{
737737
return false;
738738
}

src/Presentation/SmartStore.Web.Framework/Extensions/HttpExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ internal static void SetUserThemeChoiceInCookie(this HttpContextBase context, st
215215

216216
internal static HttpCookie GetPreviewModeCookie(this HttpContextBase context, bool createIfMissing)
217217
{
218-
if (context == null)
218+
if (context?.Handler == null)
219219
return null;
220220

221-
var cookie = context.Request.Cookies.Get("sm.PreviewModeOverrides");
221+
var cookie = context.Request?.Cookies?.Get("sm.PreviewModeOverrides");
222222

223223
if (cookie == null && createIfMissing)
224224
{

src/Presentation/SmartStore.Web.Framework/Theming/ThemeContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public string GetRequestTheme()
147147
{
148148
try
149149
{
150-
return (string)_httpContext.Request.RequestContext.RouteData.DataTokens[OverriddenThemeNameKey];
150+
return (string)_httpContext.Request?.RequestContext?.RouteData?.DataTokens[OverriddenThemeNameKey];
151151
}
152152
catch
153153
{

src/Presentation/SmartStore.Web.Framework/WebStoreContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ public void SetRequestStore(int? storeId)
5252
{
5353
try
5454
{
55-
var value = _httpContext.Request.RequestContext.RouteData?.DataTokens?.Get(OverriddenStoreIdKey);
55+
// Reduce thrown exceptions in console
56+
if (_httpContext.Handler == null)
57+
return null;
58+
59+
var value = _httpContext.Request?.RequestContext?.RouteData?.DataTokens?.Get(OverriddenStoreIdKey);
5660
if (value != null)
5761
{
5862
return (int)value;

0 commit comments

Comments
 (0)