1. 程式人生 > 實用技巧 >C#, WebApi log4net 踩坑

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")));,可行