Web Development Blog

jQuery isChildOf - is an element a child of another element

Here i s a really quick note on creating a basic extension to jQuery to check if an element is a child of another element.

jQuery Function


  
(function($) {  
    $.fn.extend({  
        isChildOf: function (filter) {  
            return $(filter).find(this).length > 0;  
        }  
    });  
})(jQuery);   
  

Example HTML

Consider the simple HTML below.


 
<div id="parent"
  <div> 
    <id="child"
    </p> 
  </div> 
</div> 
  

Usage

Some basic usage of the function.

 
if($('#child').isChildOf('#parent')){ 
 .... 
  






NHibernate with logging throws a Security Exception under Medium Trust

I am using NHibernate 3.2.0 under Medium Trust and all was going well until I turned on logging to try and trace a SQL error. It seems that when you set the log4net logging level to INFO that under a medium trust NHibernate throws a Security Exception.

Here is the Security Exception.

[SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]     System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0     System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark) +31     System.Security.CodeAccessPermission.Demand() +46     System.Reflection.RuntimeAssembly.VerifyCodeBaseDiscovery(String codeBase) +118     System.Reflection.RuntimeAssembly.GetName(Boolean copiedName) +69     System.Reflection.Assembly.GetName() +12     NHibernate.Cfg.Environment.get_Version() +114     NHibernate.Cfg.Environment..cctor() +130  

 

You can see from the stack trace exactly where the error was occurring, its quite clear. In the NHibernate source the static constructor for the Environment you can see that the NHibernate Version is logged, I was picking that is where the the error was occurring.

 

        static Environment() 
        { 
            // Computing the version string is a bit expensive, so do it only if logging is enabled. 
            if (log.IsInfoEnabled) 
            { 
                log.Info("NHibernate " + Version); 
            } 
 
            GlobalProperties = new Dictionary<string, string>(); 
            GlobalProperties[PropertyUseReflectionOptimizer] = bool.TrueString; 
            LoadGlobalPropertiesFromAppConfig(); 
            VerifyProperties(GlobalProperties); 
 
            BytecodeProviderInstance = BuildBytecodeProvider(GlobalProperties); 
            EnableReflectionOptimizer = PropertiesHelper.GetBoolean(PropertyUseReflectionOptimizer, GlobalProperties); 
 
            if (EnableReflectionOptimizer) 
            { 
                log.Info("Using reflection optimizer"); 
            } 
        } 

 

So what is wrong with accessing the Version property?

  

        public static string Version 
        { 
            get 
            { 
                if (cachedVersion == null) 
                { 
                    Assembly thisAssembly = Assembly.GetExecutingAssembly(); 
                    var attrs = 
                        (AssemblyInformationalVersionAttribute[]) 
                        thisAssembly.GetCustomAttributes(typeof (AssemblyInformationalVersionAttribute), false); 
 
                    if (attrs != null && attrs.Length > 0) 
                    { 
                        cachedVersion = string.Format("{0} ({1})", thisAssembly.GetName().Version, attrs[0].InformationalVersion); 
                    } 
                    else 
                    { 
                        cachedVersion = thisAssembly.GetName().Version.ToString(); 
                    } 
                } 
                return cachedVersion; 
            } 
        } 

 

So it seemed the Security Exception was being thrown on the call to Assembly.ExecutingAssembly()

After quite a bit of searching Google it turned out Phil Haack had also come across the problem and had written a really good account of what the problem is and the solution.

Rather than compile my custom version of NHibernate version I decided just to debug my NHibernate HQL under under Full Trust and change it back to Medium Trust when I had sorted out my problems.

Problem getting log4net 1.2.10 working with ASP.Net 4

Just a quick note on getting log4net 1.2.10 working with ASP.Net 4.

I had been working with log4net version 1.2.10.0 in ASP.Net 3.5 and it had been working but after upgrading the project to ASP.NET 4 I noticed an odd behavior. It would log only one message to the log file and refused to log any further messages until the application was restarted.

log4net Configuration File

Here is a the log4net configuration I was using which is in a separate file in the root directory

<?xml version="1.0" encoding="UTF-8"?> 
<log4net> 
    <logger name="Application"
        <level value="ERROR" /> 
        <appender-ref ref="GeneralAppender" /> 
    </logger> 
    <logger name="NHibernate"
        <level value="ERROR" /> 
        <appender-ref ref="NHibernateAppender" /> 
    </logger> 
    <appender name="GeneralAppender" type="log4net.Appender.RollingFileAppender"
        <threshold value="ERROR" /> 
        <file value="App_Data/Logs/error.log" /> 
        <appendToFile value="true" /> 
        <rollingStyle value="Date" /> 
        <datePattern value="'.'yyyyMMdd'.log'" /> 
        <layout type="log4net.Layout.PatternLayout"
            <conversionPattern value="%d %-4r [%t] %-5p %c %x - %m%n"/> 
        </layout> 
    </appender> 
    <appender name="NHibernateAppender" type="log4net.Appender.RollingFileAppender"
        <threshold value="ERROR" /> 
        <file value="App_Data/Logs/nhibernate.log" /> 
        <appendToFile value="true" /> 
        <rollingStyle value="Date" /> 
        <datePattern value="'.'yyyyMMdd'.log'" /> 
        <layout type="log4net.Layout.PatternLayout"
            <conversionPattern value="%d %-4r [%t] %-5p %c %x - %m%n"/> 
        </layout> 
    </appender> 
</log4net> 
 

Loading the Configuration

As recommended the configuration is loaded on the application start event.

private void Application_Start() 
      var configFile = new FileInfo(Server.MapPath("~/log4net.config")); 
      XmlConfigurator.Configure(configFile); 
 

After excessive searching Google and trying to find a solution I came up empty.

In the end the solution was quite straight forward I just downloaded the 1.2.11.0 release and it just worked. So I'm guessing 1.2.10.0 just isn't compatible with ASP.Net 4, or at least my project.