<commit_before>﻿using System;
using System.Web;
using Glimpse.Core.Extensibility;
using Glimpse.Core.Framework;

namespace Glimpse.AspNet
{
    public class HttpModule : IHttpModule  
    {
        private readonly Factory factory = new Factory(new AspNetServiceLocator());

        public void Init(HttpApplication httpApplication)
        {
            Init(WithTestable(httpApplication));
        }

        internal void Init(HttpApplicationBase httpApplication)
        {
            var runtime = GetRuntime(httpApplication.Application);

            AppDomain.CurrentDomain.SetData(Constants.LoggerKey, factory.InstantiateLogger());

            if (runtime.IsInitialized || runtime.Initialize())
            {
                httpApplication.BeginRequest += (context, e) => BeginRequest(WithTestable(context));
                httpApplication.PostAcquireRequestState += (context, e) => BeginSessionAccess(WithTestable(context));
                httpApplication.PostRequestHandlerExecute += (context, e) => EndSessionAccess(WithTestable(context));
                httpApplication.PostReleaseRequestState += (context, e) => EndRequest(WithTestable(context));
                AppDomain.CurrentDomain.DomainUnload += UnloadDomain;
            }
        }

        internal void UnloadDomain(object sender, EventArgs e)
        {
            var appDomain = sender as AppDomain;
            var logger = appDomain.GetData(Constants.LoggerKey) as ILogger;

            if (logger != null)
            {
                logger.Fatal("App domain with Id: '{0}' and BaseDirectory: '{1}' has been unloaded. Any in memory data stores have been lost.", appDomain.Id, appDomain.BaseDirectory);
            }
        }

        public void Dispose()
        {
            // Nothing to dispose
        }

        internal IGlimpseRuntime GetRuntime(HttpApplicationStateBase applicationState)
        {
            var runtime = applicationState[Constants.RuntimeKey] as IGlimpseRuntime;<commit_msg>Added reason for appDomain unload to log message. This improve the work already done against issue #214<commit_after>﻿using System;
using System.Reflection;
using System.Web;
using Glimpse.Core.Extensibility;
using Glimpse.Core.Framework;

namespace Glimpse.AspNet
{
    public class HttpModule : IHttpModule  
    {
        private readonly Factory factory = new Factory(new AspNetServiceLocator());

        public void Init(HttpApplication httpApplication)
        {
            Init(WithTestable(httpApplication));
        }

        internal void Init(HttpApplicationBase httpApplication)
        {
            var runtime = GetRuntime(httpApplication.Application);

            AppDomain.CurrentDomain.SetData(Constants.LoggerKey, factory.InstantiateLogger());

            if (runtime.IsInitialized || runtime.Initialize())
            {
                httpApplication.BeginRequest += (context, e) => BeginRequest(WithTestable(context));
                httpApplication.PostAcquireRequestState += (context, e) => BeginSessionAccess(WithTestable(context));
                httpApplication.PostRequestHandlerExecute += (context, e) => EndSessionAccess(WithTestable(context));
                httpApplication.PostReleaseRequestState += (context, e) => EndRequest(WithTestable(context));
                AppDomain.CurrentDomain.DomainUnload += UnloadDomain;
            }
        }

        internal void UnloadDomain(object sender, EventArgs e)
        {
            var appDomain = sender as AppDomain;
            var logger = appDomain.GetData(Constants.LoggerKey) as ILogger;
            string shutDownMessage = "Reason for shutdown: ";
            var httpRuntimeType = typeof(HttpRuntime);

            // Get shutdown message from HttpRuntime via ScottGu: http://weblogs.asp.net/scottgu/archive/2005/12/14/433194.aspx
            var httpRuntime = httpRuntimeType.InvokeMember("_theRuntime", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null) as HttpRuntime;
            if (httpRuntime != null)
            {
                shutDownMessage += httpRuntimeType.InvokeMember("_shutDownMessage", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, httpRuntime, null) as string;
            }
            else
            {
                shutDownMessage += "unknown.";
            }

            if (logger != null)
            {
                logger.Fatal("App domain with Id: '{0}' and BaseDirectory: '{1}' has been unloaded. Any in memory data stores have been lost.{2}", appDomain.Id, appDomain.BaseDirectory, shutDownMessage);
            }
        }

        public void Dispose()
        {
            // Nothing to dispose
        }

        internal IGlimpseRuntime GetRuntime(HttpApplicationStateBase applicationState)
        {
            var runtime = applicationState[Constants.RuntimeKey] as IGlimpseRuntime;
{'fraction_non_alphanumeric': 0.0866721177432543, 'fraction_numerical': 0.004497138184791497, 'mean_word_length': 2.540520984081042, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4608046', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1173, 'n_words': 283}