ASP零:System.IO.FileNotFoundException:找不到文件'c:\ windows \ system32 \ inetsrv \ log4net.config'

时间:2018-07-14 13:48:32

标签: c# asp.net-mvc aspnetboilerplate

我有一个Web项目在更新某些软件包之前仍可以正常工作 然后这行代码

遇到了运行时错误
    //Log4Net configuration
    AbpBootstrapper.IocManager.IocContainer
        .AddFacility<LoggingFacility>(f => f.UseAbpLog4Net()
            .WithConfig(Server.MapPath("log4net.config"))
        );

错误消息指出即使文件存在,“找不到文件”。

任何答案将不胜感激。 预先感谢。

1 个答案:

答案 0 :(得分:1)

对于AspNet Core,您可以使用HostingEnvironment.ContentRootPath

//Log4Net configuration
AbpBootstrapper
   .IocManager
   .IocContainer
   .AddFacility<LoggingFacility>(f => f.UseAbpLog4Net()
          .WithConfig(Path.Combine(_hostingEnvironment.ContentRootPath, "log4net.config")));

PS:您需要注入IHostingEnvironment

 private readonly IHostingEnvironment _hostingEnvironment;

 public Startup(IHostingEnvironment env)
 {              
    _hostingEnvironment = env;
 }

Update-1:用于AspNet MVC

看看HttpRuntime.AppDomainAppPath


Update-2:对于AspNet MVC-使用tilda作为MapPath

Server.MapPath("~\log4net.config")

请参见https://www.roelvanlisdonk.nl/2009/05/25/asp-net-could-not-find-a-part-of-the-path-cwindowssystem32inetsrv/