How to write code like a Pro using dotpeek / how to create and publish a nuget package / building my own Sitecore application logger

  Create a Class Library (.NET Framework) project:


Create new project:



Since I'm going to write my own logger inspired by what is in the Unicorn assembly, I pick the code using dotpeek:


Since there are missing references, I need to add the needed references:



Based on dotpeek, the Unicorn assembly uses a reference to Sitecore.Kernel dll, 


But, I want to keep this logger a new one with its own interface. So, I pick the interface definition too and add it to the class library project:



I add the Sitecore Kernel reference since that is the assembly performing the core logging functionality:



Now, there is a missing ExceptionFormatter:


Similar to Visual Studio, right click on the class and you should be able to get to the implementation using dotpeek!


Since my logger just needs FormatExceptionAsText method, I retain just that one and its dependencies:


I still see some squiggly lines, I add the necessary dependencies!

IExceptionFormatter interface:



TemplateMissingFieldExceptionFormatter class:



Note that the TemplateMissingFieldException class in the above code is picked from Rainbow.Storage.Sc.Deserialization namespace and through dotpeek,  it is easy to find what version of Rainbow.Storage.Sc assembly:


 
Crack on with the nuget package reference:


DeserializationExceptionFormatter:



Lifting DeserializationAggregationException implementation:



No squiggly lines after I add the necessary namespace references:


TemplateMissingFieldExceptionFormatter with some developer touch to optimize existing code:



Since this is my application logger, replaced relevant strings in this case:




My logger looks good after all files and references are added:


Build is successful:



Next, let's add this as a nuget package so that I can use it across my projects:

Modify Assembly Information:



Generate a nuspec file by executing nuget spec <proj name>:



Edit the .nuspec file so that it looks customized for your nuget package:




Run nuget pack to generate the .nupkg file,



 Note that initially I got the following errors since I didn't edit the default nuspec file values:


Now, we have a nuget file in the file system. Next step is to upload this package to nuget.org.

Sign into your nuget.org account , you need to register first so, I used my Microsoft account:


Was asked to register since I haven't used nuget previously:



When you click button above, you are popped with the next page to upload the package:




Select from local file system:



Once you select the package from local file system and click the submit button in the bottom of the page,  you must see the success message:




When I search for the package name, I initially didn't get it in the search results, so had to wait until later for it to be publicly available:




After few moments, it did appear in the search results:



Now, I open my Sitecore Helix sample project and in a new feature, I'm going to add my nuget package and check, here is my shiny new package:



The proof is in the pudding:




Wish to stop here a bit and say that such a logging is usually part of Sitecore Helix foundation layer but since we have created a nuget package, we are using this in a feature project. Maybe you could also use such a nuget package in the foundation project and extend that foundation project further!

Going ahead with our situation on hand, let's add some logging to one of the methods, I added it to a pipeline processor based on my previous blog so that it executes while accessing the pipelines.aspx page:



No prizes for finding the message in the Sitecore log:




Comments

Popular Posts