Skip to content

Commit

Permalink
replace IsResponse with type in JInget.Logger. TypeDescription also a…
Browse files Browse the repository at this point in the history
…dded for clarification.

add IsSuccess and IsFailure to ResponseResult in Jinget.Core.
replcae the RequestId header with HttpContext.TraceIdentifier in Jinget.Logger
  • Loading branch information
vahid committed Oct 31, 2024
1 parent c48abff commit 93100b2
Show file tree
Hide file tree
Showing 97 changed files with 612 additions and 776 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Jinget.Core.Types;

namespace Jinget.Core.ExtensionMethods.Collections;
namespace Jinget.Core.ExtensionMethods.Collections;

public static class IDictionaryExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Jinget.Core.Contracts;
using Jinget.Core.ExtensionMethods.ExpressionToSql;
using Jinget.Core.ExtensionMethods.Collections;

[assembly: InternalsVisibleTo("Jinget.Core.Tests")]
[assembly: InternalsVisibleTo("Jinget.Core.Tests")]
namespace Jinget.Core.ExtensionMethods.Database.SqlClient;

public static class IDbConnectionExtensions
Expand Down
4 changes: 1 addition & 3 deletions 01-Core/Jinget.Core/ExtensionMethods/Http/UriExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Web;

namespace Jinget.Core.ExtensionMethods.Http;
namespace Jinget.Core.ExtensionMethods.Http;

public static class UriExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System.Text.RegularExpressions;
using Jinget.Core.Attributes;
using Jinget.Core.Utilities;

namespace Jinget.Core.ExtensionMethods.Reflection;
namespace Jinget.Core.ExtensionMethods.Reflection;

public static class AssemblyExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.VisualStudio.Threading;

namespace Jinget.Core.ExtensionMethods.Reflection;
namespace Jinget.Core.ExtensionMethods.Reflection;

public static class MethodInfoExtensions
{
Expand Down
6 changes: 1 addition & 5 deletions 01-Core/Jinget.Core/Filters/SwaggerExcludeModelFilter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Jinget.Core.Attributes;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Jinget.Core.Filters;
namespace Jinget.Core.Filters;

public class SwaggerExcludeTypeFilter : IDocumentFilter
{
Expand Down
7 changes: 1 addition & 6 deletions 01-Core/Jinget.Core/Filters/SwaggerExcludePropertyFilter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Jinget.Core.Attributes;
using Jinget.Core.ExtensionMethods;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Jinget.Core.Filters;
namespace Jinget.Core.Filters;

public class SwaggerExcludePropertyFilter : ISchemaFilter
{
Expand Down
20 changes: 20 additions & 0 deletions 01-Core/Jinget.Core/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,23 @@
global using System.Threading.Tasks;
global using System.Xml.Serialization;
global using System;
global using System.Net.Mime;
global using System.Security.Cryptography;
global using System.Text.RegularExpressions;
global using System.Web;
global using Jinget.Core.Attributes;
global using Jinget.Core.Contracts;
global using Jinget.Core.ExtensionMethods;
global using Jinget.Core.ExtensionMethods.Collections;
global using Jinget.Core.ExtensionMethods.ExpressionToSql;
global using Jinget.Core.ResponseResults.Messages;
global using Jinget.Core.Security.Hashing.Model;
global using Jinget.Core.Types;
global using Jinget.Core.Utilities;
global using Microsoft.AspNetCore.Cryptography.KeyDerivation;
global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.OpenApi.Models;
global using Microsoft.VisualStudio.Threading;
global using Newtonsoft.Json.Serialization;
global using Swashbuckle.AspNetCore.SwaggerGen;
6 changes: 1 addition & 5 deletions 01-Core/Jinget.Core/ResponseResults/JingetForbiddenResult.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Jinget.Core.ResponseResults.Messages;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Jinget.Core.ResponseResults;
namespace Jinget.Core.ResponseResults;

public class JingetForbiddenResult : JsonResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Jinget.Core.ResponseResults.Messages;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Jinget.Core.ResponseResults;
namespace Jinget.Core.ResponseResults;

public class JingetUnauthorizedResult : JsonResult
{
Expand Down
4 changes: 1 addition & 3 deletions 01-Core/Jinget.Core/Security/AES/AESManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Security.Cryptography;

namespace Jinget.Core.Security.AES;
namespace Jinget.Core.Security.AES;

/// <summary>
/// Encrypt & Decrypt string using System.Security.Cryptography.RijndaelManaged algorithm
Expand Down
6 changes: 1 addition & 5 deletions 01-Core/Jinget.Core/Security/Hashing/HashManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System.Security.Cryptography;
using Jinget.Core.Security.Hashing.Model;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;

namespace Jinget.Core.Security.Hashing;
namespace Jinget.Core.Security.Hashing;

/// <summary>
/// Hash string using Pbkdf2 algorithm. This class cannot be inherited.
Expand Down
4 changes: 1 addition & 3 deletions 01-Core/Jinget.Core/Security/Hashing/IHashManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Jinget.Core.Security.Hashing.Model;

namespace Jinget.Core.Security.Hashing;
namespace Jinget.Core.Security.Hashing;

public interface IHashManager
{
Expand Down
4 changes: 1 addition & 3 deletions 01-Core/Jinget.Core/Security/SqlInjection/SqlInjection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.RegularExpressions;

namespace Jinget.Core.Security.SqlInjection;
namespace Jinget.Core.Security.SqlInjection;

/// <summary>
/// SqlInjection preventation class
Expand Down
26 changes: 21 additions & 5 deletions 01-Core/Jinget.Core/Types/ResponseResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,31 @@ public class ResponseResult<TResponseModel>
public static ResponseResult<TResponseModel> Empty => _instance ??= new ResponseResult<TResponseModel>();
public long EffectedRowsCount { get; }

/// <summary>
/// if the <typeparamref name="TResponseModel"></typeparamref> is <see cref="ProblemDetails"/> then false will be returned
/// otherwise true will be returned
/// </summary>
public bool IsSuccess => typeof(TResponseModel) != typeof(ProblemDetails);

/// <summary>
/// if the <typeparamref name="TResponseModel"></typeparamref> is <see cref="ProblemDetails"/> then true will be returned
/// otherwise false will be returned
/// </summary>
public bool IsFailure => !IsSuccess;

public List<TResponseModel> Data { get; }

public ResponseResult() { Data = []; }
public ResponseResult()
{
Data = [];
}

public ResponseResult(TResponseModel data) : this() => Data.Add(data);
public ResponseResult(IEnumerable<TResponseModel> data) : this() => Data.AddRange(data);

public ResponseResult(TResponseModel data, long effectedRowsCount) : this(data) =>
EffectedRowsCount = effectedRowsCount;

public ResponseResult(TResponseModel data, long effectedRowsCount) : this(data) => EffectedRowsCount = effectedRowsCount;
public ResponseResult(IEnumerable<TResponseModel> data, long effectedRowsCount) : this(data) => EffectedRowsCount = effectedRowsCount;

}
public ResponseResult(IEnumerable<TResponseModel> data, long effectedRowsCount) : this(data) =>
EffectedRowsCount = effectedRowsCount;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Jinget.Core.Types;
using Jinget.Core.ExtensionMethods.Collections;

namespace Jinget.Core.Utilities.Expressions;
namespace Jinget.Core.Utilities.Expressions;

public static class ExpressionUtility
{
Expand Down
4 changes: 1 addition & 3 deletions 01-Core/Jinget.Core/Utilities/Http/HeaderUtility.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Net.Mime;

namespace Jinget.Core.Utilities.Http;
namespace Jinget.Core.Utilities.Http;

public class HeaderUtility
{
Expand Down
4 changes: 1 addition & 3 deletions 01-Core/Jinget.Core/Utilities/Http/MimeTypeMap.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Net.Mime;

namespace Jinget.Core.Utilities.Http;
namespace Jinget.Core.Utilities.Http;

public static class MimeTypeMap
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Newtonsoft.Json.Serialization;

namespace Jinget.Core.Utilities.Json;
namespace Jinget.Core.Utilities.Json;

/// <summary>
/// ignore the given properties, while serializing an object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Newtonsoft.Json.Serialization;

namespace Jinget.Core.Utilities.Json;
namespace Jinget.Core.Utilities.Json;

/// <summary>
/// Serialize even properties with non public setter
Expand Down
3 changes: 1 addition & 2 deletions 01-Core/Jinget.Core/Utilities/JwtUtility.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Jinget.Core.Types;
using Microsoft.IdentityModel.Tokens;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

Expand Down
4 changes: 1 addition & 3 deletions 01-Core/Jinget.Core/Utilities/StringUtility.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Security.Cryptography;

namespace Jinget.Core.Utilities;
namespace Jinget.Core.Utilities;

public static class StringUtility
{
Expand Down
24 changes: 15 additions & 9 deletions 03-Infrastructure/Jinget.Logger/Entities/Log/LogModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace Jinget.Logger.Entities.Log;
using Jinget.Core.ExtensionMethods.Enums;

namespace Jinget.Logger.Entities.Log;

[Entity(ElasticSearchEnabled = true)]
public class LogModel : BaseEntity<long>//LogBaseEntity
public class LogModel : BaseEntity<long> //LogBaseEntity
{
public DateTime TimeStamp { get; set; }
public string Url { get; set; }
Expand All @@ -19,9 +21,9 @@ public class LogModel : BaseEntity<long>//LogBaseEntity
public string ParitionKey { get; set; }

/// <summary>
/// unique identifier for a request and response
/// unique identifier for a request and response. value is read from HttpContext.TraceIdentifier
/// </summary>
public Guid RequestId { get; set; } = Guid.Empty;
public string TraceIdentifier { get; set; }

/// <summary>
/// Http Method
Expand All @@ -44,9 +46,11 @@ public class LogModel : BaseEntity<long>//LogBaseEntity
public string IP { get; set; }

/// <summary>
/// Is the record for request or reponse?
/// Is the record for request or response?
/// </summary>
public bool IsResponse { get; set; }
public LogType Type { get; set; }

public string TypeDescription => Type.GetDescription();

/// <summary>
/// Page url initiating the request.
Expand Down Expand Up @@ -77,13 +81,15 @@ public class LogModel : BaseEntity<long>//LogBaseEntity
/// <summary>
/// This property only filled whenever calling <seealso cref="ILoggerExtensions.LogCustom"/> method.
/// </summary>
public string CallerFilePath { get; set; }
public string CallerFilePath { get; set; } = null;

/// <summary>
/// This property only filled whenever calling <seealso cref="ILoggerExtensions.LogCustom"/> method.
/// </summary>
public long CallerLineNumber { get; set; }
public long? CallerLineNumber { get; set; } = null;

/// <summary>
/// This property only filled whenever calling <seealso cref="ILoggerExtensions.LogCustom"/> method.
/// </summary>
public string CallerMember { get; set; }
public string CallerMember { get; set; } = null;
}
10 changes: 10 additions & 0 deletions 03-Infrastructure/Jinget.Logger/Enums/LogType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.ComponentModel;

namespace Jinget.Logger.Enums;

public enum LogType : byte
{
[Description("request")] Request = 1,
[Description("response")] Response = 2,
[Description("error")] Error = 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,28 @@ public void Handle(Exception ex, object details)
Details = JsonConvert.SerializeObject(details),
ex.StackTrace
}),

Type = LogType.Error,
ParitionKey =
_accessor.HttpContext.Items["jinget.log.partitionkey"] != null ?
_accessor.HttpContext.Items["jinget.log.partitionkey"].ToString() :
"",
_accessor.HttpContext.Items["jinget.log.partitionkey"] != null
? _accessor.HttpContext.Items["jinget.log.partitionkey"].ToString()
: "",
Severity = Microsoft.Extensions.Logging.LogLevel.Error.ToString(),

};
if (details is LogModel entity)
{
logEntity.RequestId = entity.RequestId;
logEntity.TraceIdentifier = entity.TraceIdentifier;
logEntity.SubSystem = entity.SubSystem;
logEntity.Url = entity.Url;
logEntity.TimeStamp = entity.TimeStamp;
}
else
{
logEntity.RequestId = new Guid(_accessor.HttpContext.Response.Headers["RequestId"].ToString());
logEntity.TraceIdentifier =
_accessor.HttpContext
.TraceIdentifier; //new Guid(_accessor.HttpContext.Response.Headers["RequestId"].ToString());
logEntity.SubSystem = AppDomain.CurrentDomain.FriendlyName;
}

_logger.LogError(JsonConvert.SerializeObject(logEntity));
return;
}
}
1 change: 1 addition & 0 deletions 03-Infrastructure/Jinget.Logger/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
global using Microsoft.Extensions.DependencyInjection.Extensions;
global using Microsoft.AspNetCore.Builder;
global using Elasticsearch.Net;
global using Jinget.Logger.Enums;
global using Jinget.Logger.Handlers;
global using Jinget.Logger.Handlers.CommandHandlers;
global using Jinget.Logger.Members;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public async Task<List<LogSearchViewModel>> SearchAsync(
f => f.Method,
f => f.PageUrl,
f => f.ParitionKey,
f => f.RequestId,
f => f.TraceIdentifier,
f => f.SubSystem,
f => f.Url,
f => f.Username))
Expand All @@ -123,12 +123,12 @@ public async Task<List<LogSearchViewModel>> SearchAsync(
: b.Must(queryContainer.ToArray());
});
})
.Aggregations(a => a.Terms("req_id", x => x.Field(f => f.RequestId.Suffix("raw"))))
.Aggregations(a => a.Terms("req_id", x => x.Field(f => f.TraceIdentifier.Suffix("raw"))))
.From((pageNumber - 1) * pageSize)
.Take(pageSize * 2)//because each operation consists of 1 req + 1 response
.Sort(s => s.Descending(d => d.TimeStamp))).ConfigureAwait(true);

var logs = searchResult.Documents.GroupBy(x => x.RequestId);
var logs = searchResult.Documents.GroupBy(x => x.TraceIdentifier);

var result = logs.Select(l => new LogSearchViewModel(l.Key, l.Select(o => o))).ToList();

Expand Down
Loading

0 comments on commit 93100b2

Please sign in to comment.