Skip to content

Commit 4aad11b

Browse files
committed
In the template C# file NuGetODataConfig.cs.pp, log exceptions to Trace (#53)
1 parent 44ce576 commit 4aad11b

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/NuGet.Server/App_Start/NuGetODataConfig.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
using System.Net.Http;
55
using System.Web.Http;
6+
using System.Web.Http.ExceptionHandling;
67
using System.Web.Http.Routing;
78
using NuGet.Server.DataServices;
9+
using NuGet.Server.Infrastructure;
810
using NuGet.Server.V2;
911

1012
// The consuming project executes this logic with its own copy of this class. This is done with a .pp file that is
@@ -28,6 +30,11 @@ public static void Initialize(HttpConfiguration config, string controllerName)
2830
{
2931
NuGetV2WebApiEnabler.UseNuGetV2WebApiFeed(config, "NuGetDefault", "nuget", controllerName);
3032

33+
config.Services.Replace(typeof(IExceptionLogger), new TraceExceptionLogger());
34+
35+
// Trace.Listeners.Add(new TextWriterTraceListener(HostingEnvironment.MapPath("~/NuGet.Server.log")));
36+
// Trace.AutoFlush = true;
37+
3138
config.Routes.MapHttpRoute(
3239
name: "NuGetDefault_ClearCache",
3340
routeTemplate: "nuget/clear-cache",

src/NuGet.Server/App_Start/NuGetODataConfig.cs.pp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.Net.Http;
22
using System.Web.Http;
3+
using System.Web.Http.ExceptionHandling;
34
using System.Web.Http.Routing;
45
using NuGet.Server;
6+
using NuGet.Server.Infrastructure;
57
using NuGet.Server.V2;
68

79
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof($rootnamespace$.App_Start.NuGetODataConfig), "Start")]
@@ -18,6 +20,11 @@
1820

1921
NuGetV2WebApiEnabler.UseNuGetV2WebApiFeed(config, "NuGetDefault", "nuget", "PackagesOData");
2022

23+
config.Services.Replace(typeof(IExceptionLogger), new TraceExceptionLogger());
24+
25+
// Trace.Listeners.Add(new TextWriterTraceListener(HostingEnvironment.MapPath("~/NuGet.Server.log")));
26+
// Trace.AutoFlush = true;
27+
2128
config.Routes.MapHttpRoute(
2229
name: "NuGetDefault_ClearCache",
2330
routeTemplate: "nuget/clear-cache",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Diagnostics;
5+
using System.Web.Http.ExceptionHandling;
6+
7+
namespace NuGet.Server.Infrastructure
8+
{
9+
public class TraceExceptionLogger : ExceptionLogger
10+
{
11+
public override void Log(ExceptionLoggerContext context)
12+
{
13+
Trace.TraceError(context.ExceptionContext.Exception.ToString());
14+
}
15+
}
16+
}

src/NuGet.Server/NuGet.Server.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
<Compile Include="Core\ServiceResolver.cs" />
110110
<Compile Include="Controllers\PackagesODataController.cs" />
111111
<Compile Include="Core\ServiceResolverExtensions.cs" />
112+
<Compile Include="Infrastructure\TraceExceptionLogger.cs" />
112113
<Compile Include="Infrastructure\WebConfigSettingsProvider.cs" />
113114
<Compile Include="Core\Helpers.cs" />
114115
<Compile Include="Infrastructure\PackageAuthenticationService.cs" />

0 commit comments

Comments
 (0)