Umbraco – start of source code analysis with Logging System

This is first article about umbraco’s source code analysis.
umbraco is big project using Asp.net MVC with 3338 cs file and lots of javascript/css/html.
Normally it is bigger than enterprise project and it will take long time to analysis it as well.

The first part is logging module of umbraco.

logging config is located at “umbraco.web.UI” project in “config\log4net.config”.
and core class is in “umbraco.core” project in “Logging\Loggger.cs”. lots of logger are located at “Logging” folder by the way.

A log4net.config which C# developer is very familiar with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0"?>
<log4net>
  <root>
    <priority value="Debug"/>
    <appender-ref ref="AsynchronousLog4NetAppender" />
  </root>
  <appender name="rollingFile" type="log4net.Appender.RollingFileAppender">
	  <file type="log4net.Util.PatternString" value="App_Data\Logs\UmbracoTraceLog.%property{log4net:HostName}.txt" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <maximumFileSize value="5MB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value=" %date [P%property{processId}/D%property{appDomainId}/T%thread] %-5level %logger - %message%newline" />
    </layout>
    <encoding value="utf-8" />
  </appender>
  <appender name="AsynchronousLog4NetAppender" type="Umbraco.Core.Logging.ParallelForwardingAppender,Umbraco.Core">
    <appender-ref ref="rollingFile" />
  </appender>
  <!--Here you can change the way logging works for certain namespaces  -->
  <logger name="NHibernate">
    <level value="WARN" />
  </logger>
</log4net>

I will add some comment on this log4net config xml as well later.

The call stack are following

at Umbraco.Core.CoreBootManager.InitializeLoggerResolver()
at Umbraco.Core.CoreBootManager.Initialize()
at Umbraco.Web.WebBootManager.Initialize()
at Umbraco.Core.UmbracoApplicationBase.StartApplication(Object sender, EventArgs e)

InitializeLoggerResolver() source code is:

1
2
3
4
5
6
7
8
9
        protected virtual void InitializeLoggerResolver()
        {
            LoggerResolver.Current = new LoggerResolver(ProfilingLogger == null ? Logger.CreateWithDefaultLog4NetConfiguration() : ProfilingLogger.Logger)
            {
                //This is another special resolver that needs to be resolvable before resolution is frozen
                //since it is used for profiling the application startup
                CanResolveBeforeFrozen = true
            };
        }

the key to init log4.net object and read configuration is:

1
     Logger.CreateWithDefaultLog4NetConfiguration()

After LoggerResolver has been initialized, it will be added into collection inside ResolverCollection using “ResolverCollection.Add”.

the Resolver adding is another topic. it has to call WriteLock, ReaderLock to add Resolver in thread-safe.
More information will be add to next topic.

This entry was posted in Umbraco. Bookmark the permalink.