Skip to content

Commit 47a9c46

Browse files
committed
logging
1 parent 5332fb3 commit 47a9c46

17 files changed

+92
-109
lines changed
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
using System;
22
using System.Net;
33
using System.Threading.Tasks;
4+
using ManagedCode.Communication.Extensions.Extensions;
45
using Microsoft.AspNetCore.SignalR;
56
using Microsoft.Extensions.Logging;
7+
using Microsoft.Extensions.Options;
68

79
namespace ManagedCode.Communication.Extensions;
810

911
public class CommunicationHubFilter : IHubFilter
1012
{
1113
private readonly ILogger<CommunicationHubFilter> _logger;
14+
private readonly IOptions<CommunicationOptions> _options;
1215

13-
public CommunicationHubFilter(ILogger<CommunicationHubFilter> logger)
16+
public CommunicationHubFilter(ILogger<CommunicationHubFilter> logger, IOptions<CommunicationOptions> options)
1417
{
1518
_logger = logger;
19+
_options = options;
1620
}
1721

18-
public async ValueTask<object?> InvokeMethodAsync(HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object?>> next)
22+
public async ValueTask<object?> InvokeMethodAsync(HubInvocationContext invocationContext,
23+
Func<HubInvocationContext, ValueTask<object?>> next)
1924
{
2025
try
2126
{
@@ -24,8 +29,11 @@ public CommunicationHubFilter(ILogger<CommunicationHubFilter> logger)
2429
catch (Exception ex)
2530
{
2631
_logger.LogError(ex, invocationContext.Hub.GetType().Name + "." + invocationContext.HubMethodName);
27-
var result = Result.Fail(HttpStatusCode.InternalServerError, ex.Message);
28-
return result;
32+
33+
if (_options.Value.ShowErrorDetails)
34+
return Result.Fail(HttpStatusCode.InternalServerError, ex.Message);
35+
36+
return Result.Fail(HttpStatusCode.InternalServerError, nameof(HttpStatusCode.InternalServerError));
2937
}
3038
}
3139
}
Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel.DataAnnotations;
4-
using System.Diagnostics;
5-
using System.IO;
62
using System.Net;
7-
using System.Runtime.Serialization;
8-
using System.Text.Json;
93
using System.Threading.Tasks;
4+
using ManagedCode.Communication.Extensions.Extensions;
105
using Microsoft.AspNetCore.Http;
116
using Microsoft.Extensions.Logging;
7+
using Microsoft.Extensions.Options;
128

139
namespace ManagedCode.Communication.Extensions;
1410

1511
public class CommunicationMiddleware
1612
{
1713
private readonly ILogger<CommunicationMiddleware> _logger;
1814
private readonly RequestDelegate _next;
15+
private readonly IOptions<CommunicationOptions> _options;
1916

20-
public CommunicationMiddleware(ILogger<CommunicationMiddleware> logger, RequestDelegate next)
17+
public CommunicationMiddleware(ILogger<CommunicationMiddleware> logger, RequestDelegate next,
18+
IOptions<CommunicationOptions> options)
2119
{
2220
_logger = logger;
2321
_next = next;
22+
_options = options;
2423
}
2524

2625
public async Task Invoke(HttpContext httpContext)
@@ -32,55 +31,24 @@ public async Task Invoke(HttpContext httpContext)
3231
catch (Exception ex)
3332
{
3433
_logger.LogError(ex, httpContext.Request.Method + "::" + httpContext.Request.Path);
35-
34+
3635
if (httpContext.Response.HasStarted)
37-
throw;
38-
36+
throw;
37+
3938
httpContext.Response.Headers.CacheControl = "no-cache,no-store";
4039
httpContext.Response.Headers.Pragma = "no-cache";
4140
httpContext.Response.Headers.Expires = "-1";
4241
httpContext.Response.Headers.ETag = default;
43-
44-
httpContext.Response.ContentType = "application/json; charset=utf-8";
45-
httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
46-
47-
var result = Result.Fail(HttpStatusCode.InternalServerError, ex.Message);
48-
await httpContext.Response.WriteAsJsonAsync(result);
49-
}
5042

51-
/*catch (Exception ex) when (ex is InvalidDataException ||
52-
ex is InvalidDataContractException)
53-
{
54-
_logger.LogError("Request throw an error", ex);
43+
httpContext.Response.ContentType = "application/json; charset=utf-8";
5544
httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
56-
var result = Result.Fail(HttpStatusCode.InternalServerError, ex);
57-
var json = JsonSerializer.Serialize(result);
58-
await httpContext.Response.WriteAsJsonAsync(json);
59-
}
60-
catch (Exception ex) when (ex is ValidationException)
61-
{
62-
_logger.LogError("Request throw an error", ex);
63-
//httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
64-
var result = Result.Fail(HttpStatusCode.InternalServerError, ex.Message);
65-
var json = JsonSerializer.Serialize(result);
66-
//await httpContext.Response.WriteAsJsonAsync(json);
67-
6845

69-
var response = httpContext.Response;
70-
response.ContentType = "application/json";
71-
72-
// get the response code and message
73-
74-
response.StatusCode = (int)HttpStatusCode.InternalServerError;
75-
await response.WriteAsync(json);
46+
if (_options.Value.ShowErrorDetails)
47+
await httpContext.Response.WriteAsJsonAsync(Result.Fail(HttpStatusCode.InternalServerError,
48+
ex.Message));
49+
else
50+
await httpContext.Response.WriteAsJsonAsync(Result.Fail(HttpStatusCode.InternalServerError,
51+
nameof(HttpStatusCode.InternalServerError)));
7652
}
77-
catch (Exception ex)
78-
{
79-
_logger.LogError("Request throw an error", ex);
80-
httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
81-
var result = Result.Fail(HttpStatusCode.InternalServerError, ex.Message);
82-
var json = JsonSerializer.Serialize(result);
83-
await httpContext.Response.WriteAsJsonAsync(json);
84-
}*/
8553
}
8654
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
using System;
2-
using System.Diagnostics.CodeAnalysis;
32
using Microsoft.AspNetCore.Builder;
4-
using Microsoft.AspNetCore.Http;
5-
using Microsoft.AspNetCore.Mvc;
63

7-
namespace ManagedCode.Communication.Extensions;
4+
namespace ManagedCode.Communication.Extensions.Extensions;
85

96
public static class CommunicationAppBuilderExtensions
107
{
118
public static IApplicationBuilder UseCommunication(this IApplicationBuilder app)
129
{
1310
if (app == null)
14-
{
1511
throw new ArgumentNullException(nameof(app));
16-
}
17-
12+
1813
return app.UseMiddleware<CommunicationMiddleware>();
1914
}
2015
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ManagedCode.Communication.Extensions.Extensions;
2+
3+
public class CommunicationOptions
4+
{
5+
public bool ShowErrorDetails { get; set; } = false;
6+
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.AspNetCore.Mvc;
33

4-
namespace ManagedCode.Communication.Extensions;
4+
namespace ManagedCode.Communication.Extensions.Extensions;
55

66
public static class ControllerExtensions
77
{
88
public static IActionResult ToActionResult<T>(this Result<T> result)
99
{
10-
return result.IsSuccess ? new OkObjectResult(result.Value) : new BadRequestObjectResult(result.GetError()?.Message);
10+
return result.IsSuccess
11+
? new OkObjectResult(result.Value)
12+
: new BadRequestObjectResult(result.GetError()?.Message);
1113
}
12-
14+
1315
public static Microsoft.AspNetCore.Http.IResult ToHttpResult<T>(this Result<T> result)
1416
{
1517
return result.IsSuccess ? Results.Ok(result.Value) : Results.BadRequest(result.GetError()?.Message);
1618
}
17-
}
19+
}

ManagedCode.Communication.Extensions/Extensions/HubOptionsExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Microsoft.AspNetCore.SignalR;
22

3-
namespace ManagedCode.Communication.Extensions;
3+
namespace ManagedCode.Communication.Extensions.Extensions;
44

55
public static class HubOptionsExtensions
66
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace ManagedCode.Communication.Extensions.Extensions;
5+
6+
public static class ServiceCollectionExtensions
7+
{
8+
public static IServiceCollection AddCommunication(this IServiceCollection services,
9+
Action<CommunicationOptions> options)
10+
{
11+
services.AddOptions<CommunicationOptions>().Configure(options);
12+
return services;
13+
}
14+
}

ManagedCode.Communication.Extensions/ManagedCode.Communication.Extensions.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
<Description>Communication for .NET</Description>
1717
<PackageTags>managedcode, Communication, Result</PackageTags>
1818
</PropertyGroup>
19-
19+
2020
<ItemGroup>
21-
<ProjectReference Include="..\ManagedCode.Communication\ManagedCode.Communication.csproj" />
21+
<ProjectReference Include="..\ManagedCode.Communication\ManagedCode.Communication.csproj"/>
2222
</ItemGroup>
2323

2424
</Project>

ManagedCode.Communication.Orleans/Converters/CollectionResultTSurrogateConverter.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
namespace ManagedCode.Communication.Converters;
55

66
[RegisterConverter]
7-
public sealed class CollectionResultTSurrogateConverter<T> : IConverter<CollectionResult<T>, CollectionResultTSurrogate<T>>
7+
public sealed class
8+
CollectionResultTSurrogateConverter<T> : IConverter<CollectionResult<T>, CollectionResultTSurrogate<T>>
89
{
910
public CollectionResult<T> ConvertFromSurrogate(in CollectionResultTSurrogate<T> surrogate)
1011
{
11-
return new CollectionResult<T>(surrogate.IsSuccess, surrogate.Collection, surrogate.PageNumber, surrogate.PageSize, surrogate.TotalItems, surrogate.Errors,
12-
surrogate.InvalidObject);
12+
return new CollectionResult<T>(surrogate.IsSuccess, surrogate.Collection, surrogate.PageNumber,
13+
surrogate.PageSize, surrogate.TotalItems, surrogate.Errors, surrogate.InvalidObject);
1314
}
1415

1516
public CollectionResultTSurrogate<T> ConvertToSurrogate(in CollectionResult<T> value)
1617
{
17-
return new CollectionResultTSurrogate<T>(value.IsSuccess, value.Collection, value.PageNumber, value.PageSize, value.TotalItems, value.Errors,
18-
value.InvalidObject);
18+
return new CollectionResultTSurrogate<T>(value.IsSuccess, value.Collection, value.PageNumber, value.PageSize,
19+
value.TotalItems, value.Errors, value.InvalidObject);
1920
}
2021
}

ManagedCode.Communication.Orleans/Extensions/OrleansExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using ManagedCode.Communication.Filters;
32
using Orleans.Hosting;
43

@@ -10,6 +9,7 @@ public static ISiloBuilder UseOrleansCommunication(this ISiloBuilder builder)
109
{
1110
return builder.AddIncomingGrainCallFilter<CommunicationIncomingGrainCallFilter>();
1211
}
12+
1313
public static IClientBuilder UseOrleansCommunication(this IClientBuilder builder)
1414
{
1515
return builder.AddOutgoingGrainCallFilter<CommunicationOutgoingGrainCallFilter>();

0 commit comments

Comments
 (0)