Skip to content

Commit

Permalink
优化日志记录的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
liuhll committed Aug 2, 2024
1 parent 19247a4 commit b923ee9
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 66 deletions.
16 changes: 0 additions & 16 deletions framework/src/Silky.Core/Logging/LoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Silky.Core.Extensions.Collections.Generic;
using Microsoft.Extensions.Logging;
using Silky.Core.Exceptions;
using Silky.Core.MiniProfiler;

namespace Silky.Core.Logging
{
Expand Down Expand Up @@ -71,21 +70,6 @@ public static void LogException(this ILogger logger, Exception ex, LogLevel? lev
LogData(logger, ex, selectedLevel);
}

public static void LogWithMiniProfiler(this ILogger logger, string category, string state,
string message, bool isError = false, LogLevel? level = null, params object[] args)
{
level ??= isError ? LogLevel.Error : LogLevel.Debug;
logger.LogWithLevel(level.Value, message, args);
if (args != null && args.Any())
{
MiniProfilerHelper.Print(category, state, string.Format(message, args), isError);
}
else
{
MiniProfilerHelper.Print(category, state, message, isError);
}
}

private static void LogKnownProperties(ILogger logger, Exception exception, LogLevel logLevel)
{
if (exception is IHasErrorCode exceptionWithErrorCode)
Expand Down
1 change: 0 additions & 1 deletion framework/src/Silky.Core/Silky.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<ItemGroup>
<PackageReference Include="Autofac" Version="7.1.*" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="MiniProfiler" Version="4.3.8" />
<PackageReference Include="NetEscapades.Configuration.Yaml" Version="3.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.*" />
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Silky.Core;
using Silky.Core.Exceptions;
using Silky.Core.Extensions;
using Silky.Core.Logging;
using Silky.Core.MiniProfiler;
using Silky.Core.Runtime.Rpc;
using Silky.Core.Serialization;
using Silky.Http.Core.Configuration;
Expand Down Expand Up @@ -61,9 +61,8 @@ protected override async Task HandleCallAsyncCore(HttpContext httpContext,
if (!serviceKey.IsNullOrEmpty())
{
RpcContext.Context.SetServiceKey(serviceKey);
Logger.LogWithMiniProfiler(MiniProfileConstant.Route.Name,
MiniProfileConstant.Route.State.FindServiceKey,
$"serviceKey => {serviceKey}");
Logger.LogInformation(
$"serviceKey => {serviceKey} for serviceEntryId {serviceEntry.Id}");
}

var rpcConnection = RpcContext.Context.Connection;
Expand Down Expand Up @@ -162,9 +161,8 @@ protected override async Task HandleCallAsyncCore(HttpContext httpContext,
if (!serviceKey.IsNullOrEmpty())
{
RpcContext.Context.SetServiceKey(serviceKey);
Logger.LogWithMiniProfiler(MiniProfileConstant.Route.Name,
MiniProfileConstant.Route.State.FindServiceKey,
$"serviceKey => {serviceKey}");
Logger.LogInformation(
$"serviceKey => {serviceKey} for serviceEntryId {serviceEntryDescriptor.Id}");
}

var clientRpcEndpoint = RpcContext.Context.Connection.ClientHost;
Expand Down
9 changes: 2 additions & 7 deletions framework/src/Silky.Http.Core/HttpContextServerCallContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Silky.Core;
using Silky.Core.Exceptions;
using Silky.Core.Extensions;
using Silky.Core.Logging;
using Silky.Core.MiniProfiler;
using Silky.Core.Runtime.Rpc;
using Silky.Core.Serialization;
using Silky.Http.Core.Configuration;
using Silky.Rpc.Configuration;
using Silky.Rpc.Extensions;
using Silky.Rpc.Runtime.Server;
using Silky.Rpc.Security;
Expand Down Expand Up @@ -79,9 +75,8 @@ public void Initialize(ISystemClock? clock = null)
SilkyRpcEventSource.Log.CallStart(ServiceEntryDescriptor.Id);
var path = HttpContext.Request.Path;
var method = HttpContext.Request.Method.ToEnum<HttpMethod>();
Logger.LogWithMiniProfiler(MiniProfileConstant.Route.Name,
MiniProfileConstant.Route.State.FindServiceEntry,
$"Find the ServiceEntryDescriptor {ServiceEntryDescriptor.Id} through {path}-{method}");
Logger.LogInformation(
$"Find the ServiceEntryDescriptor {ServiceEntryDescriptor.Id} through {method} - {path}");
HttpContext.SetUserClaims();
HttpContext.SetHttpHandleAddressInfo();
RpcContext.Context.SetInvokeAttachment(AttachmentKeys.Path, path.ToString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Silky.Core.MiniProfiler
namespace Silky.Http.MiniProfiler
{
public static class MiniProfileConstant
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Microsoft.AspNetCore.Http;
using Silky.Core;
using Silky.Core.Extensions;
using StackExchange.Profiling;

namespace Silky.Core.MiniProfiler
namespace Silky.Http.MiniProfiler
{
public static class MiniProfilerHelper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Silky.Core.MiniProfiler;
using Silky.Http.MiniProfiler;

namespace Microsoft.Extensions.DependencyInjection
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using Silky.Core.DependencyInjection;
using Silky.Core.DynamicProxy;
using Silky.Core.Extensions;
using Silky.Core.Logging;
using Silky.Core.MiniProfiler;
using Silky.Rpc.Extensions;
using Silky.Rpc.Runtime.Server;

Expand Down Expand Up @@ -170,9 +168,8 @@ async Task<object> GetResultFirstFromCache(string cacheName, string cacheKey, Se
{
if (serviceEntry.IsTransactionServiceEntry())
{
Logger.LogWithMiniProfiler(MiniProfileConstant.Caching.Name,
MiniProfileConstant.Caching.State.GetCaching,
$"Cache interception is invalid in distributed transaction processing");
Logger.LogDebug(
$"Cache interception is invalid in distributed transaction processing for the serviceEntry method {serviceEntry.GetCacheName()}");

await invocation.ProceedAsync();
proceed = ProceedType.ForExec;
Expand All @@ -182,8 +179,7 @@ async Task<object> GetResultFirstFromCache(string cacheName, string cacheKey, Se
_distributedCache.SetIgnoreMultiTenancy(getCachingInterceptProvider.IgnoreMultiTenancy);
var getCacheKeyInfo = CacheKeyHelper.GetCachingInterceptKey(serviceEntry, parameters,
serviceEntry.GetGetCachingInterceptProvider(), serviceKey);
Logger.LogWithMiniProfiler(MiniProfileConstant.Caching.Name,
MiniProfileConstant.Caching.State.GetCaching,
Logger.LogDebug(
$"Ready to get data from the cache service:[cacheName=>{serviceEntry.GetCacheName()};cacheKey=>{getCacheKeyInfo.Item1}]");
if (getCacheKeyInfo.Item2)
{
Expand Down
14 changes: 3 additions & 11 deletions framework/src/Silky.Rpc/Runtime/Client/DefaultRemoteCaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
using Silky.Core;
using Silky.Core.Exceptions;
using Silky.Core.Logging;
using Silky.Core.MiniProfiler;
using Silky.Core.Runtime.Rpc;
using Silky.Core.Serialization;
using Silky.Core.Utils;
using Silky.Rpc.Endpoint;
using Silky.Rpc.Endpoint.Selector;
using Silky.Rpc.Extensions;
using Silky.Rpc.Runtime.Server;
using Silky.Rpc.Security;
using Silky.Rpc.Transport;
using Silky.Rpc.Transport.Messages;

Expand Down Expand Up @@ -51,7 +49,7 @@ public DefaultRemoteCaller(IServerManager serverManager,
{
var sp = Stopwatch.StartNew();
var messageId = GuidGenerator.CreateGuidStrWithNoUnderline();
Logger.LogWithMiniProfiler(MiniProfileConstant.Rpc.Name, MiniProfileConstant.Rpc.State.Start,
Logger.LogDebug(
"The rpc request call start{0} serviceEntryId:[{1}]",
args: new[] { Environment.NewLine, remoteInvokeMessage.ServiceEntryId });
ClientInvokeInfo? clientInvokeInfo = null;
Expand Down Expand Up @@ -83,8 +81,6 @@ public DefaultRemoteCaller(IServerManager serverManager,
catch (Exception ex)
{
sp.Stop();
Logger.LogWithMiniProfiler(MiniProfileConstant.Rpc.Name, MiniProfileConstant.Rpc.State.Fail,
$"The rpc request call failed");
_clientInvokeDiagnosticListener.TracingError(tracingTimestamp, messageId,
remoteInvokeMessage.ServiceEntryId, ex.GetExceptionStatusCode(), ex);

Expand All @@ -105,9 +101,6 @@ public DefaultRemoteCaller(IServerManager serverManager,
sp.Stop();
invokeMonitor?.ExecSuccess((remoteInvokeMessage.ServiceEntryId, selectedRpcEndpoint),
sp.Elapsed.TotalMilliseconds, clientInvokeInfo);
Logger.LogWithMiniProfiler(MiniProfileConstant.Rpc.Name,
MiniProfileConstant.Rpc.State.Success,
$"The rpc request call succeeded");
var invokeResult = remoteInvoker.RemoteResult;
_clientInvokeDiagnosticListener.TracingAfter(tracingTimestamp, messageId,
remoteInvokeMessage.ServiceEntryId, invokeResult);
Expand All @@ -117,7 +110,7 @@ public DefaultRemoteCaller(IServerManager serverManager,
private ISilkyEndpoint[] FindRpcEndpoint(RemoteInvokeMessage remoteInvokeMessage)
{
var rpcEndpoints = _serverManager.GetRpcEndpoints(remoteInvokeMessage.ServiceId, ServiceProtocol.Rpc);
if (rpcEndpoints == null || !rpcEndpoints.Any())
if (rpcEndpoints == null || !rpcEndpoints.Any())
{
throw new NotFindServiceRouteException(
$"The service routing could not be found via [{remoteInvokeMessage.ServiceId}]");
Expand Down Expand Up @@ -154,8 +147,7 @@ private ISilkyEndpoint SelectedRpcEndpoint(ISilkyEndpoint[] rpcEndpoints, ShuntS
hashKey));
}

Logger.LogWithMiniProfiler(MiniProfileConstant.Rpc.Name,
MiniProfileConstant.Rpc.State.SelectedAddress,
Logger.LogDebug(
"There are currently available service provider addresses:{0}{1}" +
"The selected service provider rpcEndpoint is:[{2}]",
args: new[]
Expand Down
21 changes: 9 additions & 12 deletions framework/src/Silky.Rpc/Runtime/Client/DefaultRemoteExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Polly;
using Silky.Core.Extensions.Collections.Generic;
using Silky.Core.Logging;
using Silky.Core.MiniProfiler;
using Silky.Core.Runtime.Rpc;
using Silky.Rpc.Endpoint.Selector;
using Silky.Rpc.Runtime.Server;
Expand Down Expand Up @@ -45,8 +42,8 @@ public async Task<object> Execute(ServiceEntry serviceEntry, object[] parameters
if (serviceEntry.GovernanceOptions.ShuntStrategy == ShuntStrategy.HashAlgorithm)
{
hashKey = GetHashKeyValue();
Logger.LogWithMiniProfiler(MiniProfileConstant.Rpc.Name, MiniProfileConstant.Rpc.State.HashKey,
$"The value of hashkey corresponding to this rpc request is:[{hashKey}]");
Logger.LogDebug(
$"The value of hashkey corresponding to this rpc request is:[{hashKey}] for serviceEntryId:[{serviceEntry.Id}]");
}

var policy = _invokePolicyBuilder.Build(serviceEntry.Id, parameters);
Expand All @@ -71,8 +68,8 @@ public async Task<object> Execute(ServiceEntryDescriptor serviceEntryDescriptor,
if (serviceEntryDescriptor.GovernanceOptions.ShuntStrategy == ShuntStrategy.HashAlgorithm)
{
hashKey = GetHashKeyValue();
Logger.LogWithMiniProfiler(MiniProfileConstant.Rpc.Name, MiniProfileConstant.Rpc.State.HashKey,
$"The value of hashkey corresponding to this rpc request is:[{hashKey}]");
Logger.LogDebug(
$"The value of hashkey corresponding to this rpc request is:[{hashKey}] for serviceEntryId:[{serviceEntryDescriptor.Id}]");
}

var policy = _invokePolicyBuilder.Build(serviceEntryDescriptor.Id);
Expand All @@ -98,8 +95,8 @@ public async Task<object> Execute(ServiceEntryDescriptor serviceEntryDescriptor,
if (serviceEntryDescriptor.GovernanceOptions.ShuntStrategy == ShuntStrategy.HashAlgorithm)
{
hashKey = GetHashKeyValue();
Logger.LogWithMiniProfiler(MiniProfileConstant.Rpc.Name, MiniProfileConstant.Rpc.State.HashKey,
$"The value of hashkey corresponding to this rpc request is:[{hashKey}]");
Logger.LogDebug(
$"The value of hashkey corresponding to this rpc request is:[{hashKey}] for serviceEntryId:[{serviceEntryDescriptor.Id}]");
}

var policy = _invokePolicyBuilder.Build(serviceEntryDescriptor.Id);
Expand All @@ -125,8 +122,8 @@ public async Task<object> Execute(ServiceEntryDescriptor serviceEntryDescriptor,
if (serviceEntryDescriptor.GovernanceOptions.ShuntStrategy == ShuntStrategy.HashAlgorithm)
{
hashKey = GetHashKeyValue();
Logger.LogWithMiniProfiler(MiniProfileConstant.Rpc.Name, MiniProfileConstant.Rpc.State.HashKey,
$"The value of hashkey corresponding to this rpc request is:[{hashKey}]");
Logger.LogDebug(
$"The value of hashkey corresponding to this rpc request is:[{hashKey}] for serviceEntryId:[{serviceEntryDescriptor.Id}]");
}

var policy = _invokePolicyBuilder.Build(serviceEntryDescriptor.Id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using Silky.Core.MiniProfiler;

namespace Silky.SkyApm.Diagnostics.Rpc.Http.Utils
{
Expand All @@ -14,7 +13,7 @@ public static class PathUtils
".png",
".gif",
".html",
MiniProfileConstant.MiniProfilerRouteBasePath
"/index-mini-profiler"
};

public static bool IsWebApiPath(string path)
Expand Down

0 comments on commit b923ee9

Please sign in to comment.