我正在处理一个应用程序,我想要记录一个异常,但它不起作用。我想要记录的错误基本上是在validateToken上,我想知道我得到的是什么异常。这是我的实现,我添加了我在nLog中使用的所有代码。
Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args).UseNLog()
.ConfigureLogging((hostingContext, logging) =>
{
// Remove all the default logging providers
logging.ClearProviders();
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddNLog().SetMinimumLevel(LogLevel.Error);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
以下是nlog.config
文件的屏幕截图:
appSetting.json
设置文件:
"NLogConnection": {
"DbProvider": "sqlserver",
"DbHost": "SQL5103.site4now.net",
"Database": "db_a7b629_testdatabase",
"User": "db_a7b629_testdatabase_admin",
"Password": "Ad*******"
},
这里是我想要记录异常的地方
public class UserProfileInfo : IUserProfileInfo
{
private readonly IHttpNetClientService _apiService;
protected readonly IOptions<AppSettingDTO.AppSettingDTO> _appSetting;
private readonly ILogger<UserProfileInfo> _logger;
public UserProfileInfo(ILogger<UserProfileInfo> logger, IHttpNetClientService HttpClient, IOptions<AppSettingDTO.AppSettingDTO> AppSettings)
{
_apiService = HttpClient;
_appSetting = AppSettings;
this._logger = logger;
}
public bool ValidateToken(string token)
{
var tokenHandler = new JwtSecurityTokenHandler();
try
{
tokenHandler.ValidateToken(token, new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = true,
ValidIssuer = _appSetting.Value.Jwt.Issuer,
ValidAudience = _appSetting.Value.Jwt.Audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_appSetting.Value.Jwt.Key)),
ClockSkew = TimeSpan.Zero
}, out SecurityToken validatedToken);
}
catch (Exception ex)
{
// this where I want to log an exception but it is not working .. and always log me an error in the txt file generated in c:\temp folder
this._logger.LogError($"---- Token Error ----", ex.Message);
return false;
}
return true;
}
}
txt文件中的错误日志
2021-10-26 13:38:28.8797 Info Message Template Auto Format enabled
2021-10-26 13:38:28.8797 Info Loading assembly: NLog.Web.AspNetCore
2021-10-26 13:38:28.9069 Info Adding target DatabaseTarget(Name=database)
2021-10-26 13:38:28.9335 Info Validating config: TargetNames=database, ConfigItems=33, FilePath=C:\Kamran Don't Delete\SAUFIK\Project Files\GMD\GMDAPI\GMDApi\bin\Debug\net5.0\NLog.config
2021-10-26 13:38:28.9582 Info Configuration initialized.
2021-10-26 13:38:28.9582 Info NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.7.9.12899. Product version: 4.7.9+e8712e62842e2d74d60fdf37cf74d743750e5ca2. GlobalAssemblyCache: False
上面的错误是在出现异常但数据库中没有我不知道的情况下登录到默认的txt文件中的,可能是我的UserProfileinfo类有问题。
发布于 2021-10-26 09:58:49
日志记录规则决定了记录器输出应该写入的位置。您的日志记录规则规定,只有名为database
的日志记录器才应该写入database
-target:
<rules>
<logger name="database" minlevel="error" writeto="database" />
</rules>
但是在您的代码中,您使用的是基于typeof(UserProfileInfo).Name
的记录器名称ILogger<UserProfileInfo>
。
如果您只想让所有错误独立于记录器名称,那么只需使用*
-wildcard:
<rules>
<logger name="*" minlevel="error" writeto="database" />
</rules>
另请参阅:https://github.com/NLog/NLog/wiki/Tutorial
另请参阅:https://github.com/nlog/NLog/wiki/Configuration-file#rules
https://stackoverflow.com/questions/69722257
复制相似问题