我正在使用 dotnet core 并创建了一个自定义异常过滤器来处理异常。我面临的问题是,如果出现异常,自定义过滤器中的 onException 方法会被调用两次。下面是代码:
public class CustomExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
// Code
base.OnException(context);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器代码是:
[CustomExceptionFilter]
public class MyController : Controller
{
// Raise an exception in any apis
}
Run Code Online (Sandbox Code Playgroud)
为什么 onException 会被调用两次?
我在我的 dotnet 核心应用程序中使用 Serilog。我已将自定义列添加到 Serilog 提供的默认列列表中。下面是我的“Serilog”配置在 appsettings.json 文件中的样子 -
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": <connectionString>
"tableName": "Log",
"autoCreateSqlTable": true,
"columnOptionsSection": {
"removeStandardColumns": [ "MessageTemplate", "Properties"], //remove the Properties column in the standard ones
"customColumns": [
{
"ColumnName": "ControllerName",
"DataType": "varchar",
"DataLength": 50
}
]
},
"timeStamp": {
"columnName": "Timestamp",
"convertToUtc": true
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
因此,我从 Serilog 创建的默认列列表中删除了“MessageTemplate”和“Properties”,并将“ControllerName”作为新列添加到表Log中,Serilog 在其中记录其数据。我想要的是,当我记录信息时,我想为“ControllerName”列提供值。如何做呢?我找到了以下解决方案:
_logger.LogInformation("{ControllerName}{Message}", "TestController", "Starting up..");
Run Code Online (Sandbox Code Playgroud)
这行代码为 ControllerName 列提供值,但消息列获取的值如下
"TestController""Starting up.." …Run Code Online (Sandbox Code Playgroud) 我正在使用VS 2013和Log4Net来记录.Net应用程序的数据。我可以创建日志。但是当我尝试删除或移动日志文件时,它说
The file is open in another process...
Run Code Online (Sandbox Code Playgroud)
即使浏览器关闭,我也会收到此消息。关闭Visual Studio IDE工具时,我只能编辑/剪切/重命名..日志文件。我怎么解决这个问题。我希望能够随时删除/编辑文件。这是web.config中使用的log4net的代码
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="c:\logs\jwhXMLDev.log" />
<appendToFile value="true" />
<!-- <layout type="log4net.Layout.XmlLayout"/>-->
<layout type="log4net.Layout.XmlLayout" />
</appender>
<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
<onlyFixPartialEventData value="true" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
Run Code Online (Sandbox Code Playgroud)