Custom Error Loggin Solution Help

Posted by Community Admin on 04-Aug-2018 13:15

Custom Error Loggin Solution Help

All Replies

Posted by Community Admin on 30-Aug-2012 00:00

I've been studying this for off and on for a few months and I haven't really gotten anywhere with it. I am trying to implement an error logging solution so that errors generated in SF will log themselves into a database. I was able to get to a point using the Enterprise Library where I could log the errors in the database. However, the system would still generated the sitefinity error log files found in /Sitefinity/Logs folder on the website. After discussing it with my client, we would like to change this default behavior so that it only logs into the text log files when the database is not available or if it fails.

I found this forum post to show how this is possible in 3.7 but its now 3 years old and we are currently using SF 5.0. Could anyoneprovide some direction and possible some code on how this could be accomplished?  I've also created a support ticket and I'll update this thread if I get a response on it.

Thanks for your time!

Thank you for your time.
Brett Whittington

Posted by Community Admin on 03-Sep-2012 00:00

Hello,

You have to change the registered type for logging through Inversion of control - ObjectFactory.ConfigureLogging. There are five types of logs - ErrorLog, Default, Trace, UpgradeTrace and Migration.

Please take a look at


Kind regards,
Ivan Dimitrov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 19-Oct-2012 00:00

Hi Ivan,
We are using Sitefnity 4.0 and we have already changed the log directory to some other location. I would like to know - 
1] Apart from Error.log , can we create Info.log file as well in order to log user actions (such as User x has access this page id :XXX XXXX XXX) or (Page :XXXXXX took 20 ms to load) etc.
2] Also, is there any API/Methods for logging any such entries through server side coding when we are using Sitefinity API for page creations.

Thanks,
Chetan 

Posted by Community Admin on 23-Oct-2012 00:00

Hello,

We don't have tracking api for now. You need to use the existing api ( to get a user info for example) and ASP.NET 4.0 framework.

Regards,
Ivan Dimitrov
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 26-Oct-2012 00:00

I was able to figure out what I originally wanted to do. My goal was to start logging Sitefinity errors into a database instead of having Sitefinity logging its error in a text file on the server. I originally wanted to use Enterprise Library as this is what Sitefinity seems to be using to log the text files. However, I had another project since my original post that required me to use log4net. So the following code on how to do this will be for log4net.

I followed the following sites to create my log4Net.config file:
logging.apache.org/.../config-examples.html

Log4Net.config

<?xml version="1.0" encoding="utf-8" ?>
 
<log4net>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
        <bufferSize value="1" />
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <connectionString value="data source=;initial catalog=;integrated security=true;persist security info=True;" />
        <commandText value="INSERT INTO Log ([ApplicationName],[Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES ('[Application Name]', @log_date, @thread, @log_level, @logger, @message, @exception)" />
        <parameter>
            <parameterName value="@application_name" />
            <dbType value="String" />
            <size value="50" />
            <layout type="log4net.Layout.ExceptionLayout" />
        </parameter>
        <parameter>
            <parameterName value="@log_date" />
            <dbType value="DateTime" />
            <layout type="log4net.Layout.RawTimeStampLayout" />
        </parameter>
        <parameter>
            <parameterName value="@thread" />
            <dbType value="String" />
            <size value="255" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%thread" />
            </layout>
        </parameter>
        <parameter>
            <parameterName value="@log_level" />
            <dbType value="String" />
            <size value="50" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%level" />
            </layout>
        </parameter>
        <parameter>
            <parameterName value="@logger" />
            <dbType value="String" />
            <size value="255" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%logger" />
            </layout>
        </parameter>
        <parameter>
            <parameterName value="@message" />
            <dbType value="String" />
            <size value="4000" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%message" />
            </layout>
        </parameter>
        <parameter>
            <parameterName value="@exception" />
            <dbType value="String" />
            <size value="2000" />
            <layout type="log4net.Layout.ExceptionLayout" />
        </parameter>
    </appender>
 
 
 
    <!-- Set the default logging level and add the active appenders -->
    <root>
        <level value="Debug" />
        <appender-ref ref="AdoNetAppender" />
    </root>
 
</log4net>

Global.asax.cs

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Web.SessionState;
using Telerik.Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Telerik.Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.Abstractions;
 
namespace SitefinityWebApp
    public class Global : System.Web.HttpApplication
    
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        protected void Application_Start(object sender, EventArgs e)
        
            //Load Log4Net.config file
            string configFilePath = Server.MapPath("~/Log4Net.config");
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(configFilePath));
 
            //Setup method to remove Sitefinity Error Logging
            ObjectFactory.Initialized += SetupSitefinityLogPath;
        
 
        protected void SetupSitefinityLogPath(object s, ExecutedEventArgs args)
        
 
            if (args.CommandName == "ConfigureLogging")
            
 
                var builder = args.Data as ConfigurationSourceBuilder;
 
                var errorLog = ((Telerik.Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings)builder.Get("loggingConfiguration")).TraceListeners.SingleOrDefault(l => l.Name == "ErrorLog") as Telerik.Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData;
 
                //Remove the error logging trace listeners so that the Sitefinity error logs are not created.
                ((Telerik.Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings)builder.Get("loggingConfiguration")).TraceListeners.Remove("ErrorLog");
                ((Telerik.Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings)builder.Get("loggingConfiguration")).TraceSources.Remove("ErrorLog");
 
            
        
 
 
        protected void Session_Start(object sender, EventArgs e)
        
 
        
 
        protected void Application_BeginRequest(object sender, EventArgs e)
        
 
        
 
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        
 
        
 
        protected void Application_Error(object sender, EventArgs e)
        
            //Log all fatal errors
            log.Fatal("An uncaught exception occurred", this.Server.GetLastError());
        
 
        protected void Session_End(object sender, EventArgs e)
        
 
        
 
        protected void Application_End(object sender, EventArgs e)
        
 
        
    

Then add log4Net as a reference in your sitefinityWebApp project and add the Global.asax file to your project.

Finally, run the following table creation script on the server/database that you want your logs to be written too.

USE [Database Name]
GO
 
/****** Object:  Table [dbo].[Log]    Script Date: 10/26/2012 12:31:44 PM ******/
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
SET ANSI_PADDING ON
GO
 
CREATE TABLE [dbo].[Log](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ApplicationName] [varchar](50) NOT NULL,
    [Date] [datetime] NOT NULL,
    [Thread] [varchar](255) NOT NULL,
    [Level] [varchar](50) NOT NULL,
    [Logger] [varchar](255) NOT NULL,
    [Message] [varchar](4000) NOT NULL,
    [Exception] [varchar](2000) NULL
) ON [PRIMARY]
 
GO
 
SET ANSI_PADDING OFF
GO



This thread is closed