Archive

Posts Tagged ‘c#’

Sitecore Logging – Log4Net – CastleWindsor

As most of you probably already know, Sitecore (until 6.6.0) use an old version of Log4Net in the Sitecore.Logging assembly, you can read more about this issue here

So there are 2 options:

1) Use Sitecore Logging as your logging strategy

2) Use CastleWindsor Logging facilities

In order to implement the second option you have to:

Implement your logging facility:

public class  XXXContainerInstaller : IWindsorInstaller
{
public void Install(Castle.Windsor.IWindsorContainer container,
Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{

//Load your Custom Configuration from a different config “using a more advanced pattern expression

container.AddFacility<Castle.Facilities.Logging.LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net).WithConfig(“log4net.xml”));

container.Register(
Component.For<XXXX.ILoggingSetup>()
.Named(“loggingSetup”)
.ImplementedBy<XXXX.Log4net.Log4NetLoggingSetup>()
.LifeStyle.Singleton,

}

}

Prepare your config:

<castle>
<facilities>
<facility id=”logging” type=”Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging” loggingApi=”log4net”/>
</facilities>
<components>
<component id=”loggingSetup” service=”XXX.ILoggingSetup, AssemblyName” type=”XXXX.Log4NetLoggingSetup, Asos.Framework” lifestyle=”AssemblyName”/>
<component id=”loggingContext” service=”XXX.ILoggingContext, AssemblyName” type=”XXXX.Log4NetLoggingContext, AssemblyName” lifestyle=”singleton”/>
</components>
</castle>

The Log4Net Section will still be required and shared between your Castle Logging and SiteCore Logging…

<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net”/>

<log4net>
<appender name=”eventLogAppender” type=”log4net.Appender.EventLogAppender, log4net” logName=”Application”>
<param name=”LogName” value=”Application”/>
<param name=”ApplicationName” value=”XXXXX”/>
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%appdomain [%thread] %-5level – %message%newline [%properties]%newline %exception”/>
</layout>
</appender>
<root>
<level value=”ERROR”/>
<appender-ref ref=”eventLogAppender”/>
</root>
</log4net>

Be also aware that the SitecoreLogging does not support the latest log4net formatting syntax like %exception

and Sitecore formatting expression prefer the OldLog4Net syntax with all abbreviated letters

<conversionPattern value=”%4t %d{ABSOLUTE} %-5p %m%n” />

More references about this issue can be found:

http://devlicio.us/blogs/casey/archive/2008/06/18/logging-with-castle-windsor-the-logging-facility-and-log4net.aspx

http://damikulik.blogspot.co.uk/2011/05/integrate-sitecorelogging-with-castle.html

How To Create a Custom Culture in Sitecore

1) Create a culture on the server and register it

http://msdn.microsoft.com/en-us/library/ms172469(v=vs.80).aspx

CultureAndRegionInfoBuilder cib = new CultureAndRegionInfoBuilder( “x-en-US-sample”, CultureAndRegionModifiers.None);

CultureInfo ci = new CultureInfo(“en-US”);

cib.LoadDataFromCultureInfo(ci);

cib.Register();

2) Install a custom culture (copy files and deploy registry keys)
http://msdn.microsoft.com/en-us/library/ms404375(v=vs.85).aspx

3) Create Icon files (for the right sizes in Sitecore)

4) Compress the Icon in a zip file

in the folder sitecore\shell\Themes\Standard\CustomFlags.zip

4) Create Custom Language in Sitecore (specifying dictionary, iso codes etc)

image

Categories: DEV, Infrastructure Tags: , ,

Run your MVC controller ignoring SiteCore 6.6 Pipeline….

there are 3 Steps to do to achieve it:

1) write your controller inheriting from Controller

public class TestController : Controller

{

[ HttpGet]

public ActionResult Stelio()

{

//This throw an exception

//var footerRoot = Sitecore.Context.Database.GetItem(“/sitecore/content/Global/Footer”);

return Content( “this is a test to run an MVC controller in a SiteCore 6.6 site outside of SiteCore Context” );

}

}

2) Exlude the URL from the web.config


<setting name=”IgnoreUrlPrefixes” value=”/TestController/Stelio|/sitecore/default.aspx|/trace.axd|/webresource.axd|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.DialogHandler.aspx|/sitecore/shell/applications/content manager/telerik.web.ui.dialoghandler.aspx|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.SpellCheckHandler.axd|/Telerik.Web.UI.WebResource.axd|/sitecore/admin/upgrade/|/layouts/testing “/>

3) Register your route

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute( “{resource}.axd/{*pathInfo}”);

routes.MapRoute(

“Default”, // Route name

“{controller}/{action}/”, // URL with parameters

new { controller = “TestController”, action = “stelio” , } );

}

Remember that you cannot use SiteCore Context (Sitecore.Context will throw an exception)

Categories: DEV Tags: , , ,

Azure Set custom Cache Expiration on items stored in a blob

// cache time
var cacheMaxAge = (60 * 60);

 // 3600 second (1 hour) caching// file upload
blobFile.UploadByteArray(data.Data);

// setting Content-Type
blobFile.Properties.ContentType = contentType;

// setting cache for CDN
blobFile.Properties.CacheControl = "max-age=" + cacheMaxAge;

blobFile.SetProperties();

 

Categories: DEV Tags: ,

How To Use An External Assembly Without Referencing it

var ass = (IClass1) Assembly.LoadFrom(@”testLibrary.dll”).CreateInstance(“testLibrary.Class1”);

Categories: DEV Tags:

Tuple in .net 4.0

In .net 4.0 we have a new datatype… the Tuple… if you are familiar with software engineer, you know the idea behind it… to have a strongly typed datastructure that cannot be changed during the time…

// Instantiate using constructor 
Tuple<int, string, int> t1 = new Tuple<int, string, int>(4, "Stelio", 7);

// Instantiate using create method 
Tuple<int, int, int> t2 = Tuple.Create(2, 7, 9);
Categories: DEV Tags:

Access to an external Sharepoint List with Sharepoint Client API

Do you need to access a Sharepoint List from a NonSharepoint Application???! With Sharepoint 2010 it is extremely easy… you can benefit of the Sharepoint Client API and it would be extremely easy to control resource hosted on SharePoint external list.
Obviously, whenever it is possible, I would suggest to cache the Sharepoint List in your application cache layer to avoid performance issues….

 public static ListItemCollection GetClientList(string listPath)
  {
  string url = ConfigurationManager.AppSettings["sharepointConfigSite"];
  using (Microsoft.SharePoint.Client.ClientContext ctx = new Microsoft.SharePoint.Client.ClientContext(url))
     {
        var list = ctx.Web.Lists.GetByTitle(listPath);
       ctx.Load(list);
       CamlQuery camlQuery = new CamlQuery();
       camlQuery.ViewXml = "<View/>";
       ListItemCollection listItems = list.GetItems(camlQuery);
        ctx.Load(listItems);
        if (ConfigurationManager.AppSettings["sharepointUserConfigSite"] != null)
          {
           string username = ConfigurationManager.AppSettings["sharepointUserConfigSite"];
           string pwd = ConfigurationManager.AppSettings["sharepointPwdConfigSite"];
            string domain = ConfigurationManager.AppSettings["sharepointDomConfigSite"];
            var cred = new System.Net.NetworkCredential(username, pwd, domain);
            ctx.Credentials = cred;
           }   else  {
                    ctx.AuthenticationMode = ClientAuthenticationMode.Anonymous;
                }
            ctx.ExecuteQuery();
            return listItems;
      }
 }
Categories: DEV, Infrastructure Tags: ,