I've been trying to grab the ULS log entries as part of exception handling in a page to store the log text so a user can read it without having to get access to the server. I've tried using elevated privileges. This is on SharePoint 2013 on an application page.
No matter what, I get an exception:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SharePoint.Diagnostics.SPUsageDBScopeStore.ReadScopesForCorrelationId(Guid correlationId) at Microsoft.SharePoint.Diagnostics.SPUsageDBScopeStore.<>c__DisplayClass7.<Get>b__6() at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) at Microsoft.SharePoint.Diagnostics.SPUsageDBScopeStore.Get(Guid correlationId) at Microsoft.SharePoint.Diagnostics.SPULSRetriever.GetULSEntries(Guid correlationId) at MyPage.<>c__DisplayClass24.<GetULSInfo>b__21() at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass5.<RunWithElevatedPrivileges>b__3() at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) at MyPage.GetULSInfo()
Test code is this:
private string GetULSInfo()
{
string ulsLog = string.Empty;
try
{
Logger.LogInfoToULS("Testing info log");
Logger.LogErrorToULS("Testing Error log");
var guid = new Guid();
EventActivityIdControl(1, ref guid);
Logger.LogInfoToULS("Getting log for correlation id " + guid.ToString());
SPSecurity.RunWithElevatedPrivileges(
delegate()
{
SPULSRetriever ulsRetriever = new SPULSRetriever(20, 200, DateTime.Now.AddMinutes(-10));
IList<LogFileEntry> ulsLogEntries = ulsRetriever.GetULSEntries(guid);
if (ulsLogEntries.Count > 0)
{
StringBuilder sb = new StringBuilder();
foreach (LogFileEntry logEntry in ulsLogEntries)
{
if (logEntry.Category.StartsWith("EPS", StringComparison.InvariantCultureIgnoreCase))
{
sb.AppendLine(logEntry.Message);
}
}
ulsLog = sb.ToString();
}
});
}
catch (Exception ex)
{
Logger.LogErrorToULS("OperationManager::GetULSInfo", ex);
}
return ulsLog;It throws the exception at the GetULSEntries(guid) line. Nothing shows up in system logs.
Can anyone shed light on this? Very sparse info found with internet searches.