From 0646d1a9f1e1fd28520723a2312ac26e8af76d15 Mon Sep 17 00:00:00 2001 From: FrankH <4848285@qq.com> Date: Wed, 26 Mar 2025 11:11:54 +0800 Subject: [PATCH 1/9] Fix page ReceivedText --- src/MvcDemo/Controllers/WeixinController.cs | 15 ++++++++++----- .../WeixinViewModels/SendWeixinViewModel.cs | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/MvcDemo/Controllers/WeixinController.cs b/src/MvcDemo/Controllers/WeixinController.cs index faf2c84..4c86c67 100644 --- a/src/MvcDemo/Controllers/WeixinController.cs +++ b/src/MvcDemo/Controllers/WeixinController.cs @@ -16,7 +16,8 @@ public class WeixinController : Controller private readonly IWeixinUserApi _api; private readonly IWeixinCustomerSupportApi _csApi; private readonly IWeixinSubscriberStore _subscriberStore; - private readonly IWeixinResponseMessageStore _messageStore; + private readonly IWeixinResponseMessageStore _responseStore; + private readonly IWeixinReceivedMessageStore _messageStore; private readonly IWeixinReceivedEventStore _eventStore; public WeixinController( @@ -24,13 +25,15 @@ public WeixinController( IWeixinUserApi api, IWeixinCustomerSupportApi csApi, IWeixinSubscriberStore subscriberStore, - IWeixinResponseMessageStore messageStore, + IWeixinResponseMessageStore responseStore, + IWeixinReceivedMessageStore messageStore, IWeixinReceivedEventStore eventStore) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _api = api; _csApi = csApi; _subscriberStore = subscriberStore ?? throw new ArgumentNullException(nameof(subscriberStore)); + _responseStore=responseStore ?? throw new ArgumentNullException(nameof(responseStore)); _messageStore = messageStore ?? throw new ArgumentNullException(nameof(messageStore)); _eventStore = eventStore ?? throw new ArgumentNullException(nameof(eventStore)); } @@ -61,6 +64,7 @@ public async Task Subscribers(int? n) var subscribers = await _subscriberStore.GetItemsAsync(pageSize, pageIndex); _logger.LogDebug($"微信订阅者在数据库中共{totalRecords}条记录。"); vm.Item = subscribers; + vm.ReturnUrl=Url.Action(nameof(Subscribers), new { n }); return View(vm); } @@ -81,9 +85,10 @@ public async Task ReceivedText(int? n) var pageIndex = n.Value - 1; - var items = await _messageStore.GetItemsAsync(pageSize, pageIndex); + vm.Item = await _messageStore.GetItemsAsync(pageSize, pageIndex); + vm.ReturnUrl = Url.Action(nameof(ReceivedText), new { n }); _logger.LogDebug($"微信文本消息在数据库中共{totalRecords}条记录。"); - return View(items); + return View(vm); } public async Task SendWeixin(string openId) @@ -95,7 +100,7 @@ public async Task SendWeixin(string openId) var vm = new SendWeixinViewModel { - Received = await _messageStore.Items.Where(x => x.ToUserName == openId).ToListAsync(), + Responsed = await _responseStore.Items.Where(x => x.ToUserName == openId).ToListAsync(), OpenId = openId }; return View(vm); diff --git a/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs b/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs index a9c189d..eb132e5 100644 --- a/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs +++ b/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs @@ -7,7 +7,7 @@ namespace Demo.Models { public class SendWeixinViewModel { - public IEnumerable Received { get; set; } + public IEnumerable Responsed { get; set; } [Required] public string OpenId { get; set; } From ec975b8ff13f2b27911cf6ccafffc1a79607ab3e Mon Sep 17 00:00:00 2001 From: FrankH <4848285@qq.com> Date: Wed, 26 Mar 2025 11:19:22 +0800 Subject: [PATCH 2/9] Use IList in view --- src/Core/Demo.Data.Host/Views/Roles/Index.cshtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Demo.Data.Host/Views/Roles/Index.cshtml b/src/Core/Demo.Data.Host/Views/Roles/Index.cshtml index 0b73084..46e8e01 100644 --- a/src/Core/Demo.Data.Host/Views/Roles/Index.cshtml +++ b/src/Core/Demo.Data.Host/Views/Roles/Index.cshtml @@ -1,4 +1,4 @@ -@model IEnumerable +@model IList @{ ViewData["Title"] = "Index"; From c17077347b7136b4b3dac9227439856d0781170a Mon Sep 17 00:00:00 2001 From: FrankH <4848285@qq.com> Date: Wed, 26 Mar 2025 11:19:51 +0800 Subject: [PATCH 3/9] Use IList in view model. --- src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs b/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs index eb132e5..ef03a2e 100644 --- a/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs +++ b/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs @@ -7,7 +7,7 @@ namespace Demo.Models { public class SendWeixinViewModel { - public IEnumerable Responsed { get; set; } + public IList Responsed { get; set; } [Required] public string OpenId { get; set; } From fa57df6817e84686dd46b44382b33ac1b39c1e19 Mon Sep 17 00:00:00 2001 From: FrankH <4848285@qq.com> Date: Wed, 26 Mar 2025 13:18:50 +0800 Subject: [PATCH 4/9] Remove serilog. --- src/MvcDemo/Demo.csproj | 1 - src/MvcDemo/Program.cs | 19 ++++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/MvcDemo/Demo.csproj b/src/MvcDemo/Demo.csproj index edda96a..532399c 100644 --- a/src/MvcDemo/Demo.csproj +++ b/src/MvcDemo/Demo.csproj @@ -28,7 +28,6 @@ - diff --git a/src/MvcDemo/Program.cs b/src/MvcDemo/Program.cs index 548c2ec..0131364 100644 --- a/src/MvcDemo/Program.cs +++ b/src/MvcDemo/Program.cs @@ -2,24 +2,23 @@ using Demo.Data; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Hosting; -using Serilog; +using Microsoft.Extensions.Logging; using System; using System.Reflection; -Log.Logger = new LoggerConfiguration() - .WriteTo.Console() - .CreateLogger(); - var assembly = typeof(Program).Assembly; var assemblyName = assembly.GetName().Name; var assemblyVersion = assembly.GetName().Version?.ToString() ?? assembly.GetCustomAttribute()?.InformationalVersion; -Log.Information($"{assemblyName} v{assemblyVersion} starting up..."); +Console.WriteLine($"{assemblyName} v{assemblyVersion} starting up..."); try { var builder = WebApplication.CreateBuilder(args); + // Set default logging level to the most detailed level + builder.Logging.SetMinimumLevel(LogLevel.Trace); + var app = builder.ConfigureServices().Build() .MigrateDatabase() .SeedDatabase("demo", "demo@myvas.com") @@ -29,12 +28,10 @@ } catch (Exception ex) { - // Any unhandled exception during start-up will be caught and flushed to - // our log file or centralized log server - Log.Fatal(ex, "Host terminated for an unhandled exception occured."); + Console.WriteLine($"{assemblyName} v{assemblyVersion} terminated for an unhandled exception occured."); + Console.WriteLine(ex); } finally { - Log.Information($"{assemblyName} v{assemblyVersion} shutdown."); - Log.CloseAndFlush(); + Console.WriteLine($"{assemblyName} v{assemblyVersion} shutdown."); } From 9e2e3dab7b6a4bdb5df21465b6c1474f2f236d9e Mon Sep 17 00:00:00 2001 From: FrankH <4848285@qq.com> Date: Wed, 26 Mar 2025 13:20:54 +0800 Subject: [PATCH 5/9] Log detailed information to console in ConfigureServices --- src/MvcDemo/HostExtensions.cs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/MvcDemo/HostExtensions.cs b/src/MvcDemo/HostExtensions.cs index 81ac6ac..9cfa985 100644 --- a/src/MvcDemo/HostExtensions.cs +++ b/src/MvcDemo/HostExtensions.cs @@ -1,3 +1,6 @@ +using System; +using System.Diagnostics; +using System.Reflection; using Demo.Data; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Identity; @@ -5,6 +8,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using Myvas.AspNetCore.Authentication; using Myvas.AspNetCore.Weixin; @@ -14,6 +18,20 @@ public static class HostExtensions { public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder) { + // Build a temporary logger for me. + using var loggerFactory = LoggerFactory.Create(logging => + { + logging.AddConsole(); +#if DEBUG + logging.SetMinimumLevel(LogLevel.Trace); +#else + logging.SetMinimumLevel(LogLevel.Information); +#endif + }); + var logger = loggerFactory.CreateLogger(); + logger.LogTrace($"{MethodBase.GetCurrentMethod().Name}..."); + logger.LogDebug("Environment=" + builder.Environment.EnvironmentName); + var Configuration = builder.Configuration; builder.Services.AddControllersWithViews(); @@ -47,6 +65,10 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder o.AccessDeniedPath = "/Identity/Account/AccessDenied"; }); + logger?.LogDebug("WeixinOpen:AppId=" + Configuration["WeixinOpen:AppId"]); + logger?.LogDebug("WeixinAuth:AppId=" + Configuration["WeixinAuth:AppId"]); + logger?.LogDebug("QQConnect:AppId=" + Configuration["QQConnect:AppId"]); + builder.Services.AddAuthentication() .AddWeixinOpen(o => { @@ -74,6 +96,8 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder QQConnectScopes.do_like); }); + logger?.LogDebug("TencensSms:SdkAppId=" + Configuration["TencentSms:SdkAppId"]); + builder.Services.AddTencentSms(o => { o.SdkAppId = Configuration["TencentSms:SdkAppId"]; @@ -82,6 +106,8 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder.Services.AddViewDivert(); + logger?.LogDebug("Weixin:AppId=" + Configuration["Weixin:AppId"]); + builder.Services.AddWeixin(o => { o.AppId = Configuration["Weixin:AppId"]; @@ -111,6 +137,10 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder public static WebApplication ConfigurePipeline(this WebApplication app) { + var logger = app.Logger; + logger.LogTrace($"{MethodBase.GetCurrentMethod().Name}..."); + logger.LogDebug($"Environment={app.Environment.EnvironmentName}"); + var env = app.Environment; if (env.IsDevelopment()) { @@ -124,6 +154,8 @@ public static WebApplication ConfigurePipeline(this WebApplication app) } //app.UseHttpsRedirection(); app.UseStaticFiles(); + + logger.LogTrace("UseWeixinSite..."); app.UseWeixinSite(); app.UseRouting(); From 0491ff91e7a1021f472bead3768bdf52ebb0cdb7 Mon Sep 17 00:00:00 2001 From: FrankH <4848285@qq.com> Date: Wed, 26 Mar 2025 13:24:18 +0800 Subject: [PATCH 6/9] Log more detailed information in ConfigureServices in Release build. --- src/MvcDemo/HostExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MvcDemo/HostExtensions.cs b/src/MvcDemo/HostExtensions.cs index 9cfa985..f8e59ba 100644 --- a/src/MvcDemo/HostExtensions.cs +++ b/src/MvcDemo/HostExtensions.cs @@ -25,7 +25,7 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder #if DEBUG logging.SetMinimumLevel(LogLevel.Trace); #else - logging.SetMinimumLevel(LogLevel.Information); + logging.SetMinimumLevel(LogLevel.Debug); #endif }); var logger = loggerFactory.CreateLogger(); From fa31e400e491eb780f643b68e09c520238a18c60 Mon Sep 17 00:00:00 2001 From: FrankH <4848285@qq.com> Date: Wed, 26 Mar 2025 13:41:40 +0800 Subject: [PATCH 7/9] Adjust log info. --- src/MvcDemo/HostExtensions.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/MvcDemo/HostExtensions.cs b/src/MvcDemo/HostExtensions.cs index f8e59ba..61394ef 100644 --- a/src/MvcDemo/HostExtensions.cs +++ b/src/MvcDemo/HostExtensions.cs @@ -18,6 +18,7 @@ public static class HostExtensions { public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder) { + Debug.WriteLine("Create a temporary logger for me."); // Build a temporary logger for me. using var loggerFactory = LoggerFactory.Create(logging => { @@ -25,12 +26,12 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder #if DEBUG logging.SetMinimumLevel(LogLevel.Trace); #else - logging.SetMinimumLevel(LogLevel.Debug); + logging.SetMinimumLevel(LogLevel.LogInformation); #endif }); var logger = loggerFactory.CreateLogger(); - logger.LogTrace($"{MethodBase.GetCurrentMethod().Name}..."); - logger.LogDebug("Environment=" + builder.Environment.EnvironmentName); + logger.LogDebug($"{MethodBase.GetCurrentMethod().Name}..."); + logger.LogInformation("Environment=" + builder.Environment.EnvironmentName); var Configuration = builder.Configuration; @@ -65,9 +66,9 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder o.AccessDeniedPath = "/Identity/Account/AccessDenied"; }); - logger?.LogDebug("WeixinOpen:AppId=" + Configuration["WeixinOpen:AppId"]); - logger?.LogDebug("WeixinAuth:AppId=" + Configuration["WeixinAuth:AppId"]); - logger?.LogDebug("QQConnect:AppId=" + Configuration["QQConnect:AppId"]); + logger?.LogInformation("WeixinOpen:AppId=" + Configuration["WeixinOpen:AppId"]); + logger?.LogInformation("WeixinAuth:AppId=" + Configuration["WeixinAuth:AppId"]); + logger?.LogInformation("QQConnect:AppId=" + Configuration["QQConnect:AppId"]); builder.Services.AddAuthentication() .AddWeixinOpen(o => @@ -96,7 +97,7 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder QQConnectScopes.do_like); }); - logger?.LogDebug("TencensSms:SdkAppId=" + Configuration["TencentSms:SdkAppId"]); + logger?.LogInformation("TencensSms:SdkAppId=" + Configuration["TencentSms:SdkAppId"]); builder.Services.AddTencentSms(o => { @@ -106,7 +107,7 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder.Services.AddViewDivert(); - logger?.LogDebug("Weixin:AppId=" + Configuration["Weixin:AppId"]); + logger?.LogInformation("Weixin:AppId=" + Configuration["Weixin:AppId"]); builder.Services.AddWeixin(o => { @@ -139,7 +140,7 @@ public static WebApplication ConfigurePipeline(this WebApplication app) { var logger = app.Logger; logger.LogTrace($"{MethodBase.GetCurrentMethod().Name}..."); - logger.LogDebug($"Environment={app.Environment.EnvironmentName}"); + logger.LogInformation($"Environment={app.Environment.EnvironmentName}"); var env = app.Environment; if (env.IsDevelopment()) From aeff1be4e8d1ef22bf38a090a5242a3b8bd111af Mon Sep 17 00:00:00 2001 From: FrankH <4848285@qq.com> Date: Wed, 26 Mar 2025 13:41:59 +0800 Subject: [PATCH 8/9] Set default log level. --- src/MvcDemo/Program.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/MvcDemo/Program.cs b/src/MvcDemo/Program.cs index 0131364..4e74854 100644 --- a/src/MvcDemo/Program.cs +++ b/src/MvcDemo/Program.cs @@ -16,8 +16,12 @@ { var builder = WebApplication.CreateBuilder(args); - // Set default logging level to the most detailed level + // Set default logging level. +#if DEBUG builder.Logging.SetMinimumLevel(LogLevel.Trace); +#else + builder.Logging.SetMinimumLevel(LogLevel.Information); +#endif var app = builder.ConfigureServices().Build() .MigrateDatabase() From 88904dbe6d31cf307942f7c6d7e4bb575848515c Mon Sep 17 00:00:00 2001 From: FrankH <4848285@qq.com> Date: Wed, 26 Mar 2025 13:45:26 +0800 Subject: [PATCH 9/9] Fix typo --- src/MvcDemo/HostExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MvcDemo/HostExtensions.cs b/src/MvcDemo/HostExtensions.cs index 61394ef..f696ace 100644 --- a/src/MvcDemo/HostExtensions.cs +++ b/src/MvcDemo/HostExtensions.cs @@ -26,7 +26,7 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder #if DEBUG logging.SetMinimumLevel(LogLevel.Trace); #else - logging.SetMinimumLevel(LogLevel.LogInformation); + logging.SetMinimumLevel(LogLevel.Information); #endif }); var logger = loggerFactory.CreateLogger();