Skip to content

Commit f3996f1

Browse files
Add Better logging (#8648)
1 parent 910abdb commit f3996f1

10 files changed

Lines changed: 55 additions & 22 deletions

src/dotnet/APIView/APIViewWeb/Languages/CSharpLanguageService.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,9 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Text.RegularExpressions;
8-
using System.Threading;
9-
using System.Threading.Tasks;
108
using ApiView;
11-
using NuGet.Common;
12-
using NuGet.Packaging;
13-
using NuGet.Protocol;
14-
using NuGet.Protocol.Core.Types;
15-
using NuGet.Versioning;
169
using Microsoft.ApplicationInsights;
1710
using Microsoft.Extensions.Configuration;
18-
using System.Linq;
1911

2012
namespace APIViewWeb
2113
{
@@ -28,7 +20,7 @@ public class CSharpLanguageService : LanguageProcessor
2820
public override string ProcessName => _csharpParserToolPath;
2921
public override string VersionString { get; } = "27";
3022

31-
public CSharpLanguageService(IConfiguration configuration, TelemetryClient telemetryClient)
23+
public CSharpLanguageService(IConfiguration configuration, TelemetryClient telemetryClient) : base(telemetryClient)
3224
{
3325
_csharpParserToolPath = configuration["CSHARPPARSEREXECUTABLEPATH"];
3426
}

src/dotnet/APIView/APIViewWeb/Languages/GoLanguageService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Threading.Tasks;
99
using ApiView;
1010
using APIViewWeb.Helpers;
11+
using Microsoft.ApplicationInsights;
12+
using Microsoft.Extensions.Configuration;
1113

1214
namespace APIViewWeb
1315
{
@@ -18,6 +20,10 @@ public class GoLanguageService : LanguageProcessor
1820
public override string ProcessName { get; } = "apiviewgo";
1921
public override string VersionString { get; } = "2";
2022

23+
public GoLanguageService(TelemetryClient telemetryClient) : base(telemetryClient)
24+
{
25+
}
26+
2127
public override string GetProcessorArguments(string originalName, string tempDirectory, string jsonFilePath)
2228
{
2329
return $"\"{originalName}\" \"{jsonFilePath}\"";

src/dotnet/APIView/APIViewWeb/Languages/JavaLanguageService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System.IO;
5+
using Microsoft.ApplicationInsights;
56

67
namespace APIViewWeb
78
{
@@ -12,6 +13,10 @@ public class JavaLanguageService : LanguageProcessor
1213
public override string ProcessName { get; } = "java";
1314
public override string VersionString { get; } = "apiview-java-processor-1.31.0.jar";
1415

16+
public JavaLanguageService(TelemetryClient telemetryClient) : base(telemetryClient)
17+
{
18+
}
19+
1520
public override string GetProcessorArguments(string originalName, string tempDirectory, string jsonPath)
1621
{
1722
var jarPath = Path.Combine(

src/dotnet/APIView/APIViewWeb/Languages/JavaScriptLanguageService.cs

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

44
using System;
55
using System.IO;
6+
using Microsoft.ApplicationInsights;
67

78
namespace APIViewWeb
89
{
@@ -13,6 +14,10 @@ public class JavaScriptLanguageService : LanguageProcessor
1314
public override string ProcessName { get; } = "node";
1415
public override string VersionString { get; } = "1.0.8";
1516

17+
public JavaScriptLanguageService(TelemetryClient telemetryClient) : base(telemetryClient)
18+
{
19+
}
20+
1621
public override string GetProcessorArguments(string originalName, string tempDirectory, string jsonFilePath)
1722
{
1823
var jsPath = Path.Combine(

src/dotnet/APIView/APIViewWeb/Languages/LanguageProcessor.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4-
using System.IO.Compression;
54
using System.Threading.Tasks;
65
using ApiView;
76
using APIViewWeb.Helpers;
7+
using Microsoft.ApplicationInsights;
88

99
namespace APIViewWeb
1010
{
1111
public abstract class LanguageProcessor: LanguageService
1212
{
13+
private readonly TelemetryClient _telemetryClient;
14+
15+
public LanguageProcessor(TelemetryClient telemetryClient)
16+
{
17+
_telemetryClient = telemetryClient;
18+
}
19+
1320
public abstract string ProcessName { get; }
1421
public abstract string VersionString { get; }
1522
public abstract string GetProcessorArguments(string originalName, string tempDirectory, string jsonPath);
@@ -42,6 +49,8 @@ public override async Task<CodeFile> GetCodeFileAsync(string originalName, Strea
4249
{
4350
var arguments = GetProcessorArguments(originalName, tempDirectory, jsonFilePath);
4451
var processStartInfo = new ProcessStartInfo(ProcessName, arguments);
52+
string processErrors = String.Empty;
53+
4554
processStartInfo.WorkingDirectory = tempDirectory;
4655
processStartInfo.RedirectStandardError = true;
4756
processStartInfo.RedirectStandardOutput = true;
@@ -51,21 +60,29 @@ public override async Task<CodeFile> GetCodeFileAsync(string originalName, Strea
5160
process.WaitForExit();
5261
if (process.ExitCode != 0)
5362
{
54-
throw new InvalidOperationException(
55-
"Processor failed: " + Environment.NewLine +
63+
processErrors = "Processor failed: " + Environment.NewLine +
5664
"stdout: " + Environment.NewLine +
5765
process.StandardOutput.ReadToEnd() + Environment.NewLine +
5866
"stderr: " + Environment.NewLine +
59-
process.StandardError.ReadToEnd() + Environment.NewLine);
67+
process.StandardError.ReadToEnd() + Environment.NewLine;
68+
throw new InvalidOperationException(processErrors);
6069
}
6170
}
6271

63-
using (var codeFileStream = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read, FileShare.None))
72+
if (File.Exists(jsonFilePath))
73+
{
74+
using (var codeFileStream = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read, FileShare.None))
75+
{
76+
CodeFile codeFile = await CodeFile.DeserializeAsync(stream: codeFileStream, doTreeStyleParserDeserialization: LanguageServiceHelpers.UseTreeStyleParser(this.Name));
77+
codeFile.VersionString = VersionString;
78+
codeFile.Language = Name;
79+
return codeFile;
80+
}
81+
}
82+
else
6483
{
65-
CodeFile codeFile = await CodeFile.DeserializeAsync(stream: codeFileStream, doTreeStyleParserDeserialization: LanguageServiceHelpers.UseTreeStyleParser(this.Name));
66-
codeFile.VersionString = VersionString;
67-
codeFile.Language = Name;
68-
return codeFile;
84+
_telemetryClient.TrackTrace($"Processor failed to generate json file {jsonFilePath} with error {processErrors}");
85+
throw new InvalidOperationException($"Processor failed to generate json file {jsonFilePath}");
6986
}
7087
}
7188
finally

src/dotnet/APIView/APIViewWeb/Languages/ProtocolLanguageService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using Microsoft.ApplicationInsights;
45
using Microsoft.Extensions.Configuration;
56

67
namespace APIViewWeb
@@ -14,7 +15,7 @@ public class ProtocolLanguageService : LanguageProcessor
1415
private readonly string _protocolProcessor;
1516
public override string ProcessName => _protocolProcessor;
1617

17-
public ProtocolLanguageService(IConfiguration configuration)
18+
public ProtocolLanguageService(IConfiguration configuration, TelemetryClient telemetryClient) : base(telemetryClient)
1819
{
1920
// protocolGen is located in python's scripts path e.g. <Pythonhome>/Scripts/protocolGen
2021
// Env variable PROTOCOLPARSERPATH is set to <pythonhome>/Scripts/protocolGen where parser is located

src/dotnet/APIView/APIViewWeb/Languages/PythonLanguageService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class PythonLanguageService : LanguageProcessor
2121
public override string VersionString { get; } = "0.3.12";
2222
public override string ProcessName => _pythonExecutablePath;
2323

24-
public PythonLanguageService(IConfiguration configuration, TelemetryClient telemetryClient)
24+
public PythonLanguageService(IConfiguration configuration, TelemetryClient telemetryClient) : base(telemetryClient)
2525
{
2626
_pythonExecutablePath = configuration["PYTHONEXECUTABLEPATH"] ?? "python";
2727
_telemetryClient = telemetryClient;

src/dotnet/APIView/APIViewWeb/Languages/SwaggerLanguageService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using ApiView;
88
using APIViewWeb.Helpers;
9+
using Microsoft.ApplicationInsights;
910

1011
namespace APIViewWeb
1112
{
@@ -19,7 +20,7 @@ public class SwaggerLanguageService : LanguageProcessor
1920

2021
public override string ProcessName => throw new NotImplementedException();
2122

22-
public SwaggerLanguageService()
23+
public SwaggerLanguageService(TelemetryClient telemetryClient) : base(telemetryClient)
2324
{
2425
IsReviewGenByPipeline = true;
2526
}

src/dotnet/APIView/APIViewWeb/Languages/TypeSpecLanguageService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class TypeSpecLanguageService : LanguageProcessor
2424
public override string VersionString { get; } = "0";
2525
public override string ProcessName => throw new NotImplementedException();
2626

27-
public TypeSpecLanguageService(IConfiguration configuration, TelemetryClient telemetryClient)
27+
public TypeSpecLanguageService(IConfiguration configuration, TelemetryClient telemetryClient) : base(telemetryClient)
2828
{
2929
IsReviewGenByPipeline = true;
3030
_typeSpecSpecificPathPrefix = "/specification";

src/dotnet/APIView/APIViewWeb/Languages/XmlLanguageService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the MIT License.
33

44
using System.IO;
5+
using Microsoft.ApplicationInsights;
6+
using Microsoft.Extensions.Configuration;
57

68
namespace APIViewWeb
79
{
@@ -12,6 +14,10 @@ public class XmlLanguageService : LanguageProcessor
1214
public override string ProcessName { get; } = "java";
1315
public override string VersionString { get; } = "apiview-java-processor-1.31.0.jar";
1416

17+
public XmlLanguageService(TelemetryClient telemetryClient) : base(telemetryClient)
18+
{
19+
}
20+
1521
public override string GetProcessorArguments(string originalName, string tempDirectory, string jsonPath)
1622
{
1723
var jarPath = Path.Combine(

0 commit comments

Comments
 (0)