Sandboxed logging part 2   

Tags: SharePoint 2010, Sandboxed, Logging
Technorati Tags: , ,

In this blog post I’m going to show you how to implement the Patterns and practices logging in a sanboxed solution.

 

If you haven’t read Sandboxed logging part 1 I advise you to read it first before continuing this blog post :).

 

Logging in a Sandboxed solution.

 

I uploaded an example that you can use at our codeplex site. If you open the zip file you will see the following folder and files. It is recommended to download the example because the necessary wsp and DLL are also in the zip file :).

 

Step 1.

Create a new Empty SharePoint Solution and name it “Sandboxed.Logging.Example”.

EmptyProject


Step 2.

Check “Deploy as a sandboxed solution”.

image

 

Step 3.

Reference the following DLL files “Microsoft.Practices.ServiceLocation”,”Microsoft.Practices.SharePoint.Common”. The DLL files can be found in the zip file you downloaded from our codeplex.

image

 

Step 4.

Add a new webpart and name it “SandboxedLoggingTest”.

image

 

Step 5.

Use the following classes.

using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.Logging;

 

Step 6.

Create the following private variables.

private IServiceLocator _serviceLocator;
private ILogger _logger;

 

Step 7.

Override the OnInt method.

 protected override void OnInit(EventArgs e)
{
       base.OnInit(e);
       _serviceLocator = SharePointServiceLocator.GetCurrent();
       _logger = _serviceLocator.GetInstance();
}

Step 8.

Where going to create 2 buttons. The first one will log to the event viewer and ULS logs. The second one will only log to the ULS logs. In the “CreateChildControls” add the following code.

Button btnLogToOperations = new Button();
btnLogToOperations.Text = "LogToOperations";
btnLogToOperations.Click += new EventHandler(btnLogToOperations_Click);
Controls.Add(btnLogToOperations);
 
 
Button btnTraceToDeveloper = new Button();
btnTraceToDeveloper.Text = "TraceToDeveloper";
btnTraceToDeveloper.Click += new EventHandler(btnTraceToDeveloper_Click);
Controls.Add(btnTraceToDeveloper);

 

Step 9.

Add the following code to btnLogToOperations_Click.

LogToOperations will log the message or exception to the event viewer and the ULS Logs.

 void btnLogToOperations_Click(object sender, EventArgs e)
{
    _logger.LogToOperations("I'm int he event viewer and the ULS logs", 2159, SandboxEventSeverity.ErrorCritical, "VNTG/Webpart");
}

 

Step 10.

Add the following code to btnTraceToDeveloper_Click.

TraceToDeveloper will log the message or exception to the ULS logs.

void btnTraceToDeveloper_Click(object sender, EventArgs e)
{
    _logger.TraceToDeveloper("I'm in the ULS logs", 2159, "VNTG/Webpart");
}

Step 11.

Deploy the solution and add the webpart to a page.

image

 

Step 12.

Click on LogToOperations.

Event Viewer

image

 

ULS Logs

image

 

Step 13.

Click on TraceToDeveloper.

ULS Logs

image

 
Posted by  Gilissen Timmy  on  12/26/2011
29  Comments  |  Trackback Url  | 0  Links to this post | Bookmark this post with:        
 

Links to this post

Comments


  commented on  Monday, January 23, 2012  4:12 PM 
That's a smart awnser to a difficult question.


goswdryhn  commented on  Tuesday, January 24, 2012  9:41 AM 
LTJB7U pmlxhtltxmft


ulrtiumhclb  commented on  Wednesday, January 25, 2012  2:35 PM 
NP5COM , [url=http://tlsedbmmnzxc.com/]tlsedbmmnzxc[/url], [link=http://iizezmjalfng.com/]iizezmjalfng[/link], http://fjnpybgipjbo.com/


AlbPWBD  commented on  Friday, January 27, 2012  7:15 AM 
topamax vipau tamoxifen :-P


tigertatted  commented on  Sunday, January 29, 2012  1:10 PM 
buy accutane qojp viagra for woman %-))


wishybunny  commented on  Friday, February 17, 2012  2:36 AM 
What ever its mentioned above is working fine, After adding the code in the html edit section it is disappears. Just click on the save & exit button from the ribbon.Best RegardsAmitSharepoint Tech Lead



LacourLegend  commented on  Monday, February 20, 2012  2:38 AM 
I noticed nothing reoccurs every year on the calendar . I think I saw that there is an option to select reoccurs every year when you post an event . I think birthdays and anniversaries may need to be altered .



LadyRiven  commented on  Tuesday, February 21, 2012  2:37 AM 
Aww, love this. That sweater is fabulous (and so are you!). I have a sweater that's quite faded and several sizes too big, but it's gone through three generations of women in my family and although my mom keeps egging me to get rid of it I don't think I'll ever be able to.



Cartoxi  commented on  Thursday, February 23, 2012  3:05 AM 
Starting June 4th and every Saturday Tower Group 8pm meeting will be a step study/step doing.For those interested in going through the steps while we study the steps I'll be more than glad to assist you.Bring a 1 or 3 subject spiral notebook,pen,highlighter and your Big Book.This is going to be fun as well as a new experience.Yes We have entered the world of the Spirit.Our next function is to grow in understanding and effectiveness.



UK Dissertations  commented on  Monday, April 16, 2012  2:15 PM 
Hi, The topic that you have discussed in the post is really amazing, I think now I have a strong hold over the topic after going through the post.


Scholarship Essay Writing Service  commented on  Tuesday, April 17, 2012  5:29 PM 
Glad to know that this topic is being discuss also in this website & there are so many developers working on this segment but this is one of the best innovative idea ever seen.


Research Paper Writing Service UK  commented on  Tuesday, April 17, 2012  5:30 PM 
I seldom expect much from online informational content these days, but you really got my interest. I think this is some of the best, most unique informative article I have ever read. I’m truly impressed.


how can I make my assignment  commented on  Tuesday, April 17, 2012  5:33 PM 
Very fantastic post, i am really appreciate you for sharing this useful information with us its really helpful for me


Article submission services  commented on  Tuesday, April 17, 2012  5:40 PM 
This is one of the most incredible blogs Ive read in a very long time.  The amount of information in here is stunning. Your blog is great for anyone who wants to understand this subject more.  Great stuff; please keep it up!


ebkqvdqh  commented on  Thursday, April 19, 2012  11:23 PM 
ebkqvdqh


online dissertation writing help  commented on  Friday, April 20, 2012  7:40 PM 
This post is written in a very good manner and it includes many useful information for me. I am glad to find your unique way of writing the post.


dissertation proposal  commented on  Friday, April 20, 2012  7:43 PM 
Your article definitely worth looking through.. I must personally appreciate the time you devote to create this marvelous post. I can't wait for new updates on your site.


dissertation proposal  commented on  Friday, April 20, 2012  7:44 PM 
Your article definitely worth looking through.. I must personally appreciate the time you devote to create this marvelous post. I can't wait for new updates on your site.


MBA dissertation ideas  commented on  Friday, April 20, 2012  7:46 PM 
Well that’s an awesome article! I really like this article. It contains a lot of useful information. Now I can set up my new idea with the help of this post. It gives me a depth information around this subject. Thanks for sharing this valuable information for us.


professional dissertation provider  commented on  Friday, April 20, 2012  7:56 PM 
It would be not so simple to finish supreme legal dissertation just about this post by your own! My proposition is to detect the trustworthy dissertation writing service and other way, you have an opportunity to buy dissertation online in thesis service.


dissertation proposal online  commented on  Friday, April 20, 2012  8:01 PM 
It would be not so simple to finish supreme legal dissertation just about this post by your own! My proposition is to detect the trustworthy dissertation writing service and other way, you have an opportunity to buy dissertation online in thesis service.


dissertation buy online  commented on  Friday, April 20, 2012  8:05 PM 
The above contents you have mentioned in post is just too good that will be extremely helpful. I'll ensure that is stays under consideration, thanks for sharing the details keep updating, impatient for more posts.


mba dissertation ideas  commented on  Friday, April 20, 2012  8:08 PM 
The post is written in very a good manner and it contains very useful information.


seo bristol services  commented on  Friday, April 20, 2012  8:10 PM 
An excellent page you're having in here. Impressive page.


dpzoft  commented on  Saturday, April 21, 2012  1:39 AM 
dpzoft


wrjswren  commented on  Monday, April 23, 2012  7:06 PM 
wrjswren


ctqsse  commented on  Thursday, April 26, 2012  11:03 PM 
ctqsse


dzlzhhxx  commented on  Saturday, April 28, 2012  1:42 AM 
dzlzhhxx


Used Volkswagen Polo   commented on  Monday, May 07, 2012  8:32 AM 
This post is very informative and useful.. Thanks for sharing knowledge..

Name *:
URL:
Email:
Comments:


CAPTCHA Image Validation