asp.net-mvc-2 – ASP.NET MVC2 Ninject NLog(shared hosting?)= NullReferenceException

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-2 – ASP.NET MVC2 Ninject NLog(shared hosting?)= NullReferenceException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个基于 Tekpub Starter Site的MVC2应用程序,所以它使用Ninject进行依赖注入,NLog用于日志记录,还有一些其他库在各个地方.据我所知,这正是我的问题.

一切都在使用ASP.NET开发服务器(Cassini)的PC上精美地工作,但是当我部署到服务器(这是一个便宜的共享主机协议))时,我得到一个NullReferenceException,它似乎与实例化记录器的Ninject有关.

这是我的Global.asax.cs的相关位

protected override void OnApplicationStarted() {
    Logger.Info("App is starting"); // <-- I think this is what's causing the problem

    RegisterRoutes(RouteTable.Routes);
    //MvcContrib.Routing.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
    RegisterAllControllersIn(Assembly.GetExecutingAssembly());
    SetEngines();
}

protected override IKernel CreateKernel() {
    return Container;
}

internal class SiteModule : NinjectModule {
    public override void Load() {
        Bind<ILogger>().To<NLogLogger>();
        // and a couple of others...
    }
}

protected void Application_End() {
    Logger.Info("App is shutting down");
}

protected void Application_Error() {
    Exception lastException = Server.GetLastError();
    Logger.Fatal(lastException);
}

public ILogger Logger {
    get {
        return Container.Get<ILogger>();
        }
    }
    static IKernel _container;
    public static IKernel Container {
        get {
            if (_container == null) {
                _container = new StandardKernel(new SiteModule());
            }
            return _container;
        }
    }

nlog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

    <targets>
        <!--Useful for debugging-->
        <target name="console" xsi:type="ColoredConsole"
         layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" />

        <target name="file" xsi:type="File" fileName="${basedir}/App_Data/Site.log"
         layout="${date}: ${message}" />

        <target name="eventlog" xsi:type="EventLog" source="My App" log="Application"
        layout="${date}: ${message} ${stacktrace}" />

    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="file" />
        <!-- <logger name="*" minlevel="Fatal" writeTo="eventlog" /> -->
    </rules>
</nlog>

NLogLogger类:

public class NLogLogger:ILogger {

    private Logger _logger;

    public NLogLogger() {
        _logger = LogManager.GetCurrentClassLogger();
    }

    public void Info(string message) {
        _logger.Info(message);
    }

    // similar for Warn,Debug,etc

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   NLog.LogManager.GetCurrentClassLogger() +84
   DynamicInjector8cdfc2eb02f8497596a4704e379a4bb4(Object[] ) +40
   Ninject.Activation.Providers.StandardProvider.Create(IContext context) +319
   Ninject.Activation.Context.Resolve() +182
   Ninject.KernelBase.<Resolve>b__4(IContext context) +8
   System.Linq.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x) +32
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151
   System.Linq.<CastIterator>d__b1`1.MoveNext() +92
   System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +4187840
   Ninject.ResolutionExtensions.Get(IResolutionRoot root,IParameter[] parameters) +149
   Web.Application.get_Logger() in C:\mvcstarter\Web\Global.asax.cs:159
   Web.Application.OnApplicationStarted() in C:\mvcstarter\Web\Global.asax.cs:80
   Ninject.Web.Mvc.NinjectHttpApplication.Application_Start() +535

[HttpException (0x80004005): Object reference not set to an instance of an object.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context,HttpApplication app) +9024793
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext,HttpContext context,MethodInfo[] handlers) +131
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state,MethodInfo[] handlers,IntPtr appContext,HttpContext context) +194
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext,HttpContext context) +339
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +253

[HttpException (0x80004005): Object reference not set to an instance of an object.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8946484
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext context) +256

如果我注释了OnApplicationStarted的日志记录,我会得到一个不同的异常.我不知道为什么会发生这种情况,因为不应该发生任何日志记录,所以我不知道为什么尝试实例化记录器.

[NullReferenceException: Object reference not set to an instance of an object.]
   NLog.LogManager.GetCurrentClassLogger() +84
   DynamicInjector5ca7d21cf56b4732957e22cb1bfd8bdd(Object[] ) +40
   Ninject.Activation.Providers.StandardProvider.Create(IContext context) +319
   Ninject.Activation.Context.Resolve() +182
   Ninject.KernelBase.<Resolve>b__4(IContext context) +8
   System.Linq.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x) +32
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151
   System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +4187840
   Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) +347
   Ninject.Activation.Providers.StandardProvider.GetValue(IContext context,ITarget target) +138
   Ninject.Activation.Providers.<>c__DisplayClass2.<Create>b__1(ITarget target) +17
   System.Linq.WhereSelectArrayIterator`2.MoveNext() +85
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +325
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +78
   Ninject.Activation.Providers.StandardProvider.Create(IContext context) +306
   Ninject.Activation.Context.Resolve() +182
   Ninject.KernelBase.<Resolve>b__4(IContext context) +8
   System.Linq.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x) +32
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151
   System.Linq.<CastIterator>d__b1`1.MoveNext() +92
   System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +4187840
   Ninject.ResolutionExtensions.TryGet(IResolutionRoot root,String name,IParameter[] parameters) +261
   Ninject.Web.Mvc.NinjectControllerFactory.CreateController(RequestContext requestContext,String controllerName) +108
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController& controller,IControllerFactory& factory) +124
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback callback,Object state) +50
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,Object state) +48
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context,AsyncCallback cb,Object extraData) +16
   System.Web.Mvc.<>c__DisplayClass7.<BeginProcessRequest>b__6() +29
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func`1 func) +38
   System.Web.Mvc.ServerExecuteHttpHandlerAsyncWrapper.BeginProcessRequest(HttpContext context,Object extraData) +97
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler,TextWriter writer,Boolean preserveForm,Boolean setPrevIoUsPage,VirtualPath path,VirtualPath filePath,String physPath,Exception error,String queryStringOverride) +1508
   System.Web.HttpServerUtility.Execute(IHttpHandler handler,Boolean setPrevIoUsPage) +77
   System.Web.HttpServerUtility.Execute(IHttpHandler handler,Boolean preserveForm) +28
   System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler,Boolean preserveForm) +22
   System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper,String actionName,String controllerName,RouteValueDictionary routeValues,TextWriter textWriter) +372
   System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper htmlHelper,String controllerName) +35
   ASP.views_home_index_aspx.__RenderContent2(HtmlTextWriter __w,Control parameterContainer) in c:\HostingSpaces\<snip>\wwwroot\Views\Home\Index.aspx:8
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children) +109
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer,ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w,Control parameterContainer) in c:\HostingSpaces\<snip>\wwwroot\Views\Shared\Site.Master:48
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children) +208
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +55
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint) +3060

我唯一可以让网站工作的方法是完全伪造记录器,我完全不开心.这是我第一次使用ASP.NET共享托管,所以我有点失落.任何人都有任何有用的建议?

解决方法

您可以避免使用LogManager.GetCurrentClassLogger()并让Ninject为您解决当前的类名:

将NLogLogger类的构造函数更改为:

public NLogLogger(string currentClassName) 
{
  _logger = LogManager.GetLogger(currentClassName);
}

并更改ninject绑定以解析当前的类名称

Bind<ILogger>().To<NLogLogger>()
  .WithConstructorArgument("currentClassName",x => x.Request.ParentContext.Request.Service.FullName);

额外的好处:您的日志文件现在还包括发出日志指令的类名.

我知道我的答案有点晚了,但我今天很自豪,在任何地方找不到答案.也许这有助于别人.

原文链接:https://www.f2er.com/aspnet/250838.html

猜你在找的asp.Net相关文章