C#, WebApi log4net 踩坑

 1 public class WebApiApplication : System.Web.HttpApplication
 2     {
 3         protected void Application_Start()
 4         {
 5             log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Web.config")));
 6             AreaRegistration.RegisterAllAreas();
 7             GlobalConfiguration.Configure(WebApiConfig.Register);
 8             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
 9             RouteConfig.RegisterRoutes(RouteTable.Routes);
10             BundleConfig.RegisterBundles(BundleTable.Bundles);
11         }
12     }

关键行在于

log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Web.config")));

 

之前试过

在AssemblyInfo.cs里面添加[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)],失败;

在WebApiConfig.cs里面添加log4net.Config.XmlConfigurator.Configure();,失败;

 

最后发现

log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Web.config")));,可行

C#, WebApi log4net 踩坑

上一篇:虚拟机win10添加新磁盘


下一篇:C#报错:Could not find any resources appropriate for the specified culture or the neutral culture