简体   繁体   中英

ASP Zero :System.IO.FileNotFoundException: Could not find file 'c:\windows\system32\inetsrv\log4net.config'

I have a web project which still working properly until I ve updated some packages then I faced a run time error with this line of code

    //Log4Net configuration
    AbpBootstrapper.IocManager.IocContainer
        .AddFacility<LoggingFacility>(f => f.UseAbpLog4Net()
            .WithConfig(Server.MapPath("log4net.config"))
        );

the error message said that "Could not find file" even though the file is exist.

Any answers will be appreciated. Thanks in advance.

For AspNet Core, you can use HostingEnvironment.ContentRootPath

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

PS: You need to inject IHostingEnvironment

 private readonly IHostingEnvironment _hostingEnvironment;

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

Update-1 : for AspNet MVC

Take a look at the HttpRuntime.AppDomainAppPath


Update-2 : for AspNet MVC - Use tilda for MapPath

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM