Skip to content

Commit 92dbe3a

Browse files
committed
[fix] 修复日志写入错误导致程序异常问题
1 parent b678066 commit 92dbe3a

File tree

8 files changed

+57
-36
lines changed

8 files changed

+57
-36
lines changed

Ape.Volo.Api/Filter/GlobalExceptionFilter.cs

+17-5
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,23 @@ public async Task OnExceptionAsync(ExceptionContext context)
8989
MiniProfiler.Current.CustomTiming("Errors:", throwMsg);
9090
}
9191

92-
//记录日志
93-
_logger.LogError(WriteLog(context.HttpContext, remoteIp, ipAddress, context.Exception,
94-
_httpUser.Account,
95-
_browserDetector.Browser?.OS, _browserDetector.Browser?.DeviceType, _browserDetector.Browser?.Name,
96-
_browserDetector.Browser?.Version), context.Exception);
92+
try
93+
{
94+
//记录日志
95+
_logger.LogError(WriteLog(context.HttpContext, remoteIp, ipAddress, context.Exception,
96+
_httpUser.Account,
97+
_browserDetector.Browser?.OS, _browserDetector.Browser?.DeviceType, _browserDetector.Browser?.Name,
98+
_browserDetector.Browser?.Version));
99+
}
100+
catch (Exception e)
101+
{
102+
//_logger.LogCritical("LogError出错:" + e.ToString());
103+
FileHelper.WriteLog(WriteLog(context.HttpContext, remoteIp, ipAddress, e,
104+
_httpUser.Account,
105+
_browserDetector.Browser?.OS, _browserDetector.Browser?.DeviceType, _browserDetector.Browser?.Name,
106+
_browserDetector.Browser?.Version));
107+
}
108+
97109
var settingDto = await _settingService.FindSettingByName("IsExceptionLogSaveDB");
98110
if (settingDto != null && settingDto.Value.ToBool() && exceptionType != typeof(DemoRequestException))
99111
{

Ape.Volo.Api/appsettings.Development.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@
5353
"Enabled": true,
5454
"ConnectionString": "apevoloMaster.db"
5555
},
56-
{
57-
"ConnId": "Ape.Volo.Log",
58-
"HitRate": 0,
59-
"DBType": 2,
60-
"Enabled": true,
61-
"ConnectionString": "apevoloLog.db"
62-
},
6356
{
6457
"ConnId": "Ape.Volo.Oracle",
6558
"HitRate": 20,
6659
"DBType": 3,
6760
"Enabled": false,
6861
"ConnectionString": "Data Source=localhost/orcl;User ID=system;Password=123456;Persist Security Info=True;Connection Timeout=60;"
62+
},
63+
{
64+
"ConnId": "Ape.Volo.Log",
65+
"HitRate": 0,
66+
"DBType": 2,
67+
"Enabled": true,
68+
"ConnectionString": "apevoloLog.db"
6969
}
7070
]
7171
},

Ape.Volo.Api/appsettings.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@
5353
"Enabled": true,
5454
"ConnectionString": "apevoloMaster.db"
5555
},
56-
{
57-
"ConnId": "Ape.Volo.Log",
58-
"HitRate": 0,
59-
"DBType": 2,
60-
"Enabled": true,
61-
"ConnectionString": "apevoloLog.db"
62-
},
6356
{
6457
"ConnId": "Ape.Volo.Oracle",
6558
"HitRate": 20,
6659
"DBType": 3,
6760
"Enabled": false,
6861
"ConnectionString": "Data Source=localhost/orcl;User ID=system;Password=123456;Persist Security Info=True;Connection Timeout=60;"
62+
},
63+
{
64+
"ConnId": "Ape.Volo.Log",
65+
"HitRate": 0,
66+
"DBType": 2,
67+
"Enabled": true,
68+
"ConnectionString": "apevoloLog.db"
6969
}
7070
]
7171
},

Ape.Volo.Common/Helper/FileHelper.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Text;
55
using Ape.Volo.Common.Extention;
6+
using Ape.Volo.Common.Global;
67

78
namespace Ape.Volo.Common.Helper;
89

@@ -167,12 +168,11 @@ private static void WriteTxt(string content, string path, Encoding encoding, Fil
167168
/// 输出日志到指定文件
168169
/// </summary>
169170
/// <param name="msg">日志消息</param>
170-
/// <param name="path">日志文件位置(默认为D:\测试\a.log)</param>
171-
public static void WriteLog(string msg, string path = @"Log.txt")
171+
public static void WriteLog(string msg)
172172
{
173-
string content = $"{DateTime.Now.ToCstTime().ToString("yyyy-MM-dd HH:mm:ss")}:{msg}";
174-
175-
WriteTxt(content, $"{GetCurrentDir()}{content}");
173+
string content = $"/Logs/{DateTime.Now.ToCstTime():yyyy-MM-dd}.log";
174+
msg = "【当前时间】 : " + DateTime.Now.ToCstTime().ToString("yyyy-MM-dd HH:mm:ss") + msg;
175+
WriteText(msg, $"{AppSettings.ContentRootPath}{content}", Encoding.UTF8);
176176
}
177177

178178

Ape.Volo.Entity/Monitor/AuditLog.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public class AuditLog : BaseEntity, ISoftDeletedEntity
5353
/// <summary>
5454
/// 请求参数
5555
/// </summary>
56-
[SugarColumn(Length = 4000, IsNullable = true)]
56+
[SugarColumn(ColumnDataType = "longtext,text,clob", IsNullable = true)]
5757
public string RequestParameters { get; set; }
5858

5959
/// <summary>
6060
/// 响应数据
6161
/// </summary>
62-
[SugarColumn(Length = 4000, IsNullable = true)]
62+
[SugarColumn(ColumnDataType = "longtext,text,clob", IsNullable = true)]
6363
public string ResponseData { get; set; }
6464

6565
/// <summary>

Ape.Volo.Entity/Monitor/ExceptionLog.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class ExceptionLog : BaseEntity, ISoftDeletedEntity
5353
/// <summary>
5454
/// 请求参数
5555
/// </summary>
56-
[SugarColumn(Length = 4000, IsNullable = true)]
56+
[SugarColumn(ColumnDataType = "longtext,text,clob", IsNullable = true)]
5757
public string RequestParameters { get; set; }
5858

5959
/// <summary>
@@ -65,13 +65,13 @@ public class ExceptionLog : BaseEntity, ISoftDeletedEntity
6565
/// <summary>
6666
/// 异常完整信息
6767
/// </summary>
68-
[SugarColumn(IsNullable = true)]
68+
[SugarColumn(ColumnDataType = "longtext,text,clob", IsNullable = true)]
6969
public string ExceptionMessageFull { get; set; }
7070

7171
/// <summary>
7272
/// 异常堆栈信息
7373
/// </summary>
74-
[SugarColumn(Length = 500, IsNullable = true)]
74+
[SugarColumn(ColumnDataType = "longtext,text,clob", IsNullable = true)]
7575
public string ExceptionStack { get; set; }
7676

7777
/// <summary>

Ape.Volo.Entity/Seed/DataSeeder.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public static async Task InitSystemDataAsync(DataContext dataContext, bool isIni
4949
}
5050
else
5151
{
52-
throw new Exception("sqlsugar官方表示Oracle不支持代码建库,请先建库再启动项目");
52+
//已有库得情况下 把抛异常代码注释掉
53+
throw new Exception("sqlSugar不支持Oracle使用代码建库,请先建库后注释该代码重新启动!");
5354
}
5455

5556
ConsoleHelper.WriteLine("初始化主库成功。", ConsoleColor.Green);
@@ -441,7 +442,16 @@ await dataContext.GetEntityDb<RoleApis>().InsertRangeAsync(
441442
ConsoleHelper.WriteLine($"Log Db ConnectString: {logDb.CurrentConnectionConfig.ConnectionString}");
442443
ConsoleHelper.WriteLine("初始化日志库成功。", ConsoleColor.Green);
443444
ConsoleHelper.WriteLine("初始化日志库数据表....");
444-
logDb.DbMaintenance.CreateDatabase();
445+
if (logDb.CurrentConnectionConfig.DbType != DbType.Oracle)
446+
{
447+
logDb.DbMaintenance.CreateDatabase();
448+
}
449+
else
450+
{
451+
//已有库得情况下 把抛异常代码注释掉
452+
throw new Exception("sqlSugar不支持Oracle使用代码建库,请先建库后注释该代码重新启动!");
453+
}
454+
445455
var logEntityList = GlobalData.GetEntityAssembly().GetTypes()
446456
.Where(x => x.IsClass && x != typeof(SerilogBase) && x.Namespace != null &&
447457
x.Namespace.StartsWith("Ape.Volo.Entity.Monitor")).ToList();

Ape.Volo.IBusiness/Dto/Permission/CreateUpdateUserDto.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,24 @@ public class CreateUpdateUserDto : BaseEntityDto<long>
4646
/// <summary>
4747
/// 性别
4848
/// </summary>
49-
[Display(Name = "User.Gender")]
50-
[Required(ErrorMessage = "{0}required")]
49+
[Required]
5150
public string Gender { get; set; }
5251

5352
/// <summary>
5453
/// 部门
5554
/// </summary>
56-
[Required(ErrorMessage = "{0}required")]
55+
[Required]
5756
public UserDeptDto Dept { get; set; }
5857

5958
/// <summary>
6059
/// 角色
6160
/// </summary>
62-
[Required(ErrorMessage = "{0}required")]
61+
[Required]
6362
public List<UserRoleDto> Roles { get; set; }
6463

6564
/// <summary>
6665
/// 岗位
6766
/// </summary>
68-
[Required(ErrorMessage = "{0}required")]
67+
[Required]
6968
public List<UserJobDto> Jobs { get; set; }
7069
}

0 commit comments

Comments
 (0)