4

This is similar to "http://sharepoint.stackexchange.com/questions/2057/how-to-write-to-uls-log-in-sharepoint-2010-from-a-net-app". But in my case, logs are NOT showing up in ULS when logging from a web app.

App Pool accounts ARE ALL in the "Performance Log Users" group. Permissions on the LOGS directory seem to be correct, based on looking at other machines where logging is working.

The line of code I'm running in Page_Load of a simple application page, for testing, is:

SPDiagnosticsService.Local.WriteTrace(1234, 
    new SPDiagnosticsCategory("My Errors", TraceSeverity.Monitorable, EventSeverity.Error),
    TraceSeverity.Monitorable, "Exception occured: {0}", new object[] { "NOT!" });

When I write the same line in a console app or linqpad, it logs just fine.

Any ideas of what else I can look at to figure this out?

4
  • The web application you are referencing -is- a SharePoint site collection, right? Or are you trying to write to the uls from a standard asp.net web app?? Commented Feb 8, 2012 at 11:12
  • 1
    -- thanks to everyone who tried to help out here -- just to put this to rest, it turns out the problem was in a custom Autofac module (the SharePoint IoC container we are using) with a bad logging implementation which was locking up the SPDiagnosticsService. We fixed the implementation and everything is a go now ...
    – James
    Commented Feb 24, 2012 at 17:39
  • I face the same problem for a really long time. It feels like it's an infrastructure problem. My findings: 1) mostly, I have this problem with SharePoint 2013 Foundation 2) I have one farm with two servers one of which has this problem and the other doesn't. 3) it does not matter whether Logging category exists or not 4) We are facing this problem using both very old loggers that we used for years and with different, new ones. 5) logging with Elevated privileges changes nothing 6) running logger method from PowerShell or console it works perfectly fine, but does not run from SPLogLevel or w3wp Commented May 7, 2015 at 15:55
  • I wonder if it has something to do with a SharePoint trial period that expired... Commented May 7, 2015 at 16:37

3 Answers 3

3

One of possible reasons is that your custom category (eg. 'My Errors') and/or area doesn't exist. AFAIK you should receive error about that in ULS.

To list all available categories you can use PS and Get-SPLogLevel

Also check SPDiagnosticsServiceBase.WriteTrace Method

You must use a category that is recognized by the service. For an example showing how to query a diagnostics services for areas and categories, see the Areas property.

Keep in mind that if you pass a value in the severity parameter that is less than the currently configured value for the category's TraceSeverity property, the trace is not written to the log.

3
  • 1
    Thanks for getting back on this. The code I pasted works in other environments, just not the one I'm debugging now. For example, to get back to your point, if I use an existing area and category, it still doesn't work from the web app (does work in the console and/or linqpad). For example: var category = SPDiagnosticsService.Local.Areas["SharePoint Foundation"].Categories["General"]; SPDiagnosticsService.Local.WriteTrace(1234, category, TraceSeverity.High, "Exception occured: {0}", new object[] { "NOT!" }); It most likely has to be an infrastructure thing.
    – James
    Commented Feb 7, 2012 at 20:34
  • On thing (maybe not related¸but you never know): Instead of using Page_Load (default) try to use override OnLoad() (don't ask why) and/or put some custom button and do logging on OnClick event. This is just to eliminate possibility that problem is in your app page. Commented Feb 7, 2012 at 20:56
  • 1
    Also: The AppPool user needs to be in the Performance Log Users group
    – Dennis G
    Commented Feb 3, 2014 at 11:50
2

Have you tried running your logging code using elevated privileges?

SPSecurity.RunWithElevatedPrivileges(delegate() {
   SPDiagnosticsService.Local.WriteTrace(1234, 
       new SPDiagnosticsCategory("My Errors", TraceSeverity.Monitorable, EventSeverity.Error),
       TraceSeverity.Monitorable, "Exception occured: {0}", new object[] { "NOT!" });
});
1
  • I've tried it. it does not help at all Commented May 7, 2015 at 15:50
0

In Autofac registration module, dont use SPUtility.GetVersionedGenericSetupPath and any other methods which internally can use uls logging

This cause stopping any custom SPDiagnosticsServiceBase based loggers to stop work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.