这是我的代码。 我仅使用Microsoft.Extensions.Logging.Log4Net.AspNetCore参考 我在调试窗口中遇到以下异常。您能指导我设置log4net配置文件吗?
log4net: configuring repository [log4net-default-repository] using XML element
log4net: Configuring Repository [log4net-default-repository]
log4net: Configuration update mode [Merge].
log4net: Logger [root] Level string is [Error].
log4net: Logger [root] level set to [name="ERROR",value=70000].
log4net: Loading Appender [AuditAppender] type: [Logger.Log4NetAuditAppender]
log4net:ERROR Could not create Appender [AuditAppender] of type [Logger.Log4NetAuditAppender]. Reported error follows.
System.TypeLoadException: Could not load type 'Logger.Log4NetAuditAppender' from assembly 'log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'.
at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type, ObjectHandleOnStack keepAlive)
at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)
log4net:ERROR Appender named [AuditAppender] not found.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddLogging(builder => builder.AddLog4Net("log4net.config"));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddLog4Net();
app.UseMvc();
}
我的log4.net配置
<?xml version="1.0" encoding="utf-8"?>
<log4net debug="true">
<root>
<level value="Error" />
<appender-ref ref="AuditAppender" />
</root>
<appender name="AuditAppender" type="Logger.Log4NetAuditAppender,Logger">
</appender>
</log4net>
我的自定义附加程序
public class Log4NetAuditAppender : AppenderSkeleton
{
public Log4NetAuditAppender(){
}
protected override void Append(LoggingEvent loggingEvent)
{
Console.WriteLine("coming inside");
}
}
}
我的控制器
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private ILogger<ValuesController> _logger;
public ValuesController(ILogger<ValuesController> logger) { _logger = logger;}
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
_logger.LogInformation("test", null);
return "value";
}
答案 0 :(得分:0)
我有同样的问题, 将程序集名称添加到附加器标记中的完整类名称之后,即可解决该问题。
<appender name="AuditAppender" type="Logger.Log4NetAuditAppender,Logger">
只需将“,Logger”替换为,它就可以工作。