Skip to content

Commit 8a91397

Browse files
committed
Add logger class for easy reuse and clean up code
1 parent 93464f4 commit 8a91397

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

src/Microsoft.OpenApi.Hidi/Logger.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace Microsoft.OpenApi.Hidi
7+
{
8+
public class Logger
9+
{
10+
public static ILogger<OpenApiService> ConfigureLogger(LogLevel logLevel)
11+
{
12+
// Configure logger options
13+
#if DEBUG
14+
logLevel = logLevel > LogLevel.Debug ? LogLevel.Debug : logLevel;
15+
#endif
16+
17+
using var loggerFactory = LoggerFactory.Create((builder) =>
18+
{
19+
builder
20+
.AddSimpleConsole(c =>
21+
{
22+
c.IncludeScopes = true;
23+
})
24+
#if DEBUG
25+
.AddDebug()
26+
#endif
27+
.SetMinimumLevel(logLevel);
28+
});
29+
30+
var logger = loggerFactory.CreateLogger<OpenApiService>();
31+
32+
return logger;
33+
}
34+
}
35+
}

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
using System.Threading;
2727
using System.Xml.Xsl;
2828
using System.Xml;
29-
using System.Runtime.CompilerServices;
3029
using System.Reflection;
3130

3231
namespace Microsoft.OpenApi.Hidi
@@ -36,7 +35,7 @@ public class OpenApiService
3635
/// <summary>
3736
/// Implementation of the transform command
3837
/// </summary>
39-
public static async Task<int> TransformOpenApiDocument(
38+
public static async Task TransformOpenApiDocument(
4039
string openapi,
4140
string csdl,
4241
string csdlFilter,
@@ -54,8 +53,7 @@ public static async Task<int> TransformOpenApiDocument(
5453
CancellationToken cancellationToken
5554
)
5655
{
57-
using var loggerFactory = ConfigureLoggerInstance(loglevel);
58-
var logger = loggerFactory.CreateLogger<OpenApiService>();
56+
var logger = Logger.ConfigureLogger(loglevel);
5957

6058
try
6159
{
@@ -212,18 +210,11 @@ CancellationToken cancellationToken
212210
logger.LogTrace($"Finished serializing in {stopwatch.ElapsedMilliseconds}ms");
213211
textWriter.Flush();
214212
}
215-
return 0;
216213
}
217214
catch (Exception ex)
218215
{
219-
#if DEBUG
220-
logger.LogCritical(ex, ex.Message);
221-
#else
222-
logger.LogCritical(ex.Message);
223-
224-
#endif
225-
return 1;
226-
}
216+
throw new InvalidOperationException($"Could not transform the document, reason: {ex.Message}", ex);
217+
}
227218
}
228219

229220
private static XslCompiledTransform GetFilterTransform()
@@ -249,18 +240,15 @@ private static Stream ApplyFilter(string csdl, string entitySetOrSingleton, XslC
249240
return stream;
250241
}
251242

252-
253-
254243
/// <summary>
255244
/// Implementation of the validate command
256245
/// </summary>
257-
public static async Task<int> ValidateOpenApiDocument(
246+
public static async Task ValidateOpenApiDocument(
258247
string openapi,
259248
LogLevel loglevel,
260249
CancellationToken cancellationToken)
261250
{
262-
using var loggerFactory = ConfigureLoggerInstance(loglevel);
263-
var logger = loggerFactory.CreateLogger<OpenApiService>();
251+
var logger = Logger.ConfigureLogger(loglevel);
264252

265253
try
266254
{
@@ -308,19 +296,11 @@ public static async Task<int> ValidateOpenApiDocument(
308296
logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report..");
309297
logger.LogInformation(statsVisitor.GetStatisticsReport());
310298
}
311-
312-
return 0;
313299
}
314300
catch (Exception ex)
315301
{
316-
#if DEBUG
317-
logger.LogCritical(ex, ex.Message);
318-
#else
319-
logger.LogCritical(ex.Message);
320-
#endif
321-
return 1;
302+
throw new InvalidOperationException($"Could not validate the document, reason: {ex.Message}", ex);
322303
}
323-
324304
}
325305

326306
/// <summary>

0 commit comments

Comments
 (0)