简体   繁体   中英

Exception occuring in ASP.Net MVC app with Log4net on Shared Hosting

I have a website that was developed using ASP.Net MVC and uses Log4net for exception logging. The website was running fine until about 4 hours ago. We haven't made any changes to the website in the last 2 weeks. The website has been running smoothly since we put it up about 1 month ago. The website is hosted at our ISP on a Shared Hosting account.

This is the exception we are receiving:

[FormatException: String was not recognized as a valid Boolean.]
   log4net.Core.LoggerManager.RegisterAppDomainEvents() +0
   log4net.Core.LoggerManager..cctor() +33

[TypeInitializationException: The type initializer for 'log4net.Core.LoggerManager' threw an exception.]
   log4net.Core.LoggerManager.GetLogger(Assembly repositoryAssembly, String name) +0
   log4net.LogManager.GetLogger(Assembly repositoryAssembly, String name) +8
   log4net.LogManager.GetLogger(String name) +20
   i3t.LcaWeb.View.MvcApplication..cctor() in Global.asax.cs:17

[TypeInitializationException: The type initializer for 'i3t.LcaWeb.View.MvcApplication' threw an exception.]
   i3t.LcaWeb.View.MvcApplication..ctor() +0
   ASP.global_asax..ctor() in App_global.asax.fqjmwqjv.0.cs:0

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) +86
   System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +230
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +67
   System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +1051
   System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +111
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +215
   System.Web.HttpApplicationFactory.FireApplicationOnStart(HttpContext context) +8878568
   System.Web.HttpApplicationFactory.EnsureAppStartCalled(HttpContext context) +136
   System.Web.HttpApplicationFactory.GetApplicationInstance(HttpContext context) +92
   System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +289

The exception is being thrown in the Global.asax, where an instance of log4net.ILog is being created:

public class MvcApplication : System.Web.HttpApplication
{
    private static log4net.ILog log = log4net.LogManager.GetLogger("global.asax");

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Home.aspx",
            "",
            new { action = "Index", controller = "Home" }
        );

The problem doesn't happen in testing - we are unable to reproduce it. I have deleted all the files on the live website, and re-uploaded them, just in case something could have been corrupted, but this hasn't solved the problem.

I believe that something might have changed on the ISP side that broke something.

Any clues on how to solve this problem?

I had a similar problem. In my situation it turned out that I had accidentally filled out a boolean field in the Web.config as something that was not a boolean.

For example I had this in my Web.config:

<appender name="XYLogger" type="log4net.Appender.RollingFileAppender">
    <file value="App_Data\logging\xymaps.log" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <appendToFile value="some-file-name"/>
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="2" />
    <maximumFileSize value="2MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-tp %c %m%n" />
    </layout>
</appender>

I had mistaken <appendToFile> as a tag for specifying the file name the logger should append to. Instead it should have read <appendToFile value="true" /> . So if anyone else encounters this issue I would advise them to double check their Web.config for boolean fields that have incorrect values.

LogManager.GetLogger() expects name of the class in which it will be used for logging and not Global.asax, which is the name of a file. Please use just "Global" as the parameter to LogManager.GetLogger().

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