Kaynağa Gözat

回填社区信息

qinchaoyue 4 ay önce
ebeveyn
işleme
4f0c58a728

+ 2 - 2
src/Hotline.Api/StartupExtensions.cs

@@ -133,8 +133,8 @@ internal static class StartupExtensions
         //mediatr
         services.RegisterMediatR(appConfiguration);
 
-        services.AddSingleton<LoggingInterceptor>();
-        services.AddSingleton<AsyncLoggingInterceptor>();
+        services.AddScoped<LoggingInterceptor>();
+        services.AddScoped<AsyncLoggingInterceptor>();
 
         //app scope
         switch (appConfiguration.AppScope)

+ 8 - 8
src/Hotline.Application.Tests/Controller/DefaultSessionContext.cs

@@ -37,7 +37,7 @@ public class DefaultSessionContext : ISessionContext, IScopeDependency
     /// </summary>
     public string? UserId
     {
-        get { return _contextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); }
+        get { return _contextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier); }
         init { }
     }
 
@@ -48,7 +48,7 @@ public class DefaultSessionContext : ISessionContext, IScopeDependency
     public string RequiredUserId => _contextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
     public string? UserName
     {
-        get { return _contextAccessor.HttpContext.User.FindFirstValue(AppClaimTypes.UserDisplayName); }
+        get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.UserDisplayName); }
         init { }
     }
 
@@ -63,26 +63,26 @@ public class DefaultSessionContext : ISessionContext, IScopeDependency
     /// </summary>
     public string[] Roles
     {
-        get { return _contextAccessor.HttpContext.User.Claims.Where(d => d.Type == ClaimTypes.Role).Select(d => d.Value).ToArray(); }
+        get { return _contextAccessor.HttpContext?.User.Claims.Where(d => d.Type == ClaimTypes.Role).Select(d => d.Value).ToArray(); }
         init { }
     }
 
     public string? OrgId
     {
-        get { return _contextAccessor.HttpContext.User.FindFirstValue(AppClaimTypes.DepartmentId); }
+        get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId); }
         init { }
     }
 
-    public string RequiredOrgId => _contextAccessor.HttpContext.User.FindFirstValue(AppClaimTypes.DepartmentId);
+    public string RequiredOrgId => _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId);
     public string? OrgName
     {
-        get { return _contextAccessor.HttpContext.User.FindFirstValue(AppClaimTypes.DepartmentName); }
+        get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentName); }
         init { }
     }
 
     public int OrgLevel
     {
-        get { return _contextAccessor.HttpContext.User.FindIntValue(AppClaimTypes.DepartmentLevel); }
+        get { return _contextAccessor.HttpContext?.User.FindIntValue(AppClaimTypes.DepartmentLevel) ?? 0; }
         init { }
     }
 
@@ -109,7 +109,7 @@ public class DefaultSessionContext : ISessionContext, IScopeDependency
 
     public string? AreaId
     {
-        get { return _contextAccessor.HttpContext.User.FindFirstValue(AppClaimTypes.AreaId); }
+        get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.AreaId); }
         init { }
     }
     public string? ClientId

+ 5 - 4
src/Hotline.Application.Tests/Startup.cs

@@ -119,8 +119,6 @@ public class Startup
             services.AddSqlSugar(configuration);
             services.AddCAPDb(configuration);
 
-            // application services
-            services.AddScoped<ISnapshotApplication, ZiGongSnapshotApplication>();
 
             //mq
             services.AddTestMq(configuration);
@@ -188,8 +186,11 @@ public class Startup
             services.AddScoped<IFileDomainService, FileDomainService>();
             services.AddXingTangDb(callCenterConfiguration.XingTang);
             services.AddScoped<IGuiderSystemService, TiqnQueService>();
-            services.AddSingleton<LoggingInterceptor>();
-            services.AddSingleton<AsyncLoggingInterceptor>();
+            services.AddScoped<LoggingInterceptor>();
+            services.AddScoped<AsyncLoggingInterceptor>();
+
+            // application services
+            services.AddProxiedScoped<ISnapshotApplication, DefaultSnapshotApplication>();
             //ServiceLocator.Instance = services.BuildServiceProvider();
         }
 

+ 15 - 0
src/Hotline/Settings/SystemLog.cs

@@ -54,5 +54,20 @@ namespace Hotline.Settings
 		///</summary>
 		[SugarColumn(ColumnDataType = "json", IsJson = true)]
 		public object? ExecuteResult { get; set; }
+
+		/// <summary>
+		/// 开始时间
+		/// </summary>
+		public DateTime? StartTime { get; set; }
+
+		/// <summary>
+		/// 结束时间
+		/// </summary>
+		public DateTime? EndTime { get; set; }
+
+		/// <summary>
+		/// 耗时(秒)
+		/// </summary>
+		public int? Interval { get; set; }
 	}
 }

+ 3 - 0
src/Hotline/Settings/SystemLogDomain/AsyncLoggingInterceptor.cs

@@ -66,6 +66,7 @@ public class AsyncLoggingInterceptor : AsyncInterceptorBase
         var entity = new SystemLog
         {
             Name = invocation.Method.Name,
+            StartTime = DateTime.Now
         };
         for (int i = 0;i < invocation.Arguments.Length;i++)
         {
@@ -92,6 +93,7 @@ public class AsyncLoggingInterceptor : AsyncInterceptorBase
     {
         if (logEntity != null)
         {
+            logEntity.EndTime = DateTime.Now;
             logEntity.ExecuteResult = result;
             _capPublisher.Publish(EventNames.LoggingInterceptor, logEntity);
         }
@@ -110,6 +112,7 @@ public class LoggingInterceptorHandler : ICapSubscribe, ITransientDependency
     [CapSubscribe(EventNames.LoggingInterceptor)]
     public async Task Handle(SystemLog logEntity)
     {
+        logEntity.Interval = (int)(logEntity.EndTime.Value - logEntity.StartTime.Value).TotalSeconds;
         await _systemLogRepository.AddAsync(logEntity);
     }
 }