Skip to content

Instantly share code, notes, and snippets.

@vman
Last active December 20, 2016 13:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vman/9358016 to your computer and use it in GitHub Desktop.
Save vman/9358016 to your computer and use it in GitHub Desktop.
View Logs in SharePoint Online
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using System;
using System.Security;
namespace ViewSPOLog
{
class Program
{
static void Main(string[] args)
{
//Open the Tenant Administration Context with the Tenant Admin Url
using (var tenantContext = new ClientContext("https://yoursite-admin.sharepoint.com/"))
{
//Authenticate with a Tenant Administrator
var passWord = new SecureString();
foreach (char c in "password".ToCharArray()) passWord.AppendChar(c);
tenantContext.Credentials = new SharePointOnlineCredentials("admin@yoursite.onmicrosoft.com", passWord);
var tenant = new Tenant(tenantContext);
var tenantLog = new TenantLog(tenantContext);
var dateTimeUTCNow = DateTime.UtcNow;
//Get 50 Rows of Tenant Log Entries starting from 5 days ago till now.
var logEntries = tenantLog.GetEntries(dateTimeUTCNow.AddDays(-5), dateTimeUTCNow, 50);
//Get 50 Rows of Tenant Log Entries of the specified CorrelationId starting from 5 days ago till now
//var logEntries = tenantLog.GetEntriesByCorrelationId(dateTimeUTCNow.AddDays(-5), dateTimeUTCNow, 50, new Guid("ae2b1e34-12eb-4652-a0db-ce4ab916c74e"));
//Get 50 Rows of Tenant Log Entries of the specified Source starting from 5 days ago till now.
//var logEntries = tenantLog.GetEntriesBySource(dateTimeUTCNow.AddDays(-5), dateTimeUTCNow, 50, 1);
//Get 50 Rows of Tenant Log Entries of the specified User starting from 5 days ago till now.
//var logEntries = tenantLog.GetEntriesByUser(dateTimeUTCNow.AddDays(-5), dateTimeUTCNow, 50, "admin@yoursite.onmicrosoft.com");
tenantContext.Load(logEntries);
tenantContext.ExecuteQuery();
foreach (TenantLogEntry logEntry in logEntries)
{
Console.WriteLine(string.Format("Timestamp:{0} | Message:{1} | CorrelationId:{2} | Source:{3} | User:{4} | CategoryId:{5}",
logEntry.TimestampUtc, logEntry.Message, logEntry.CorrelationId , logEntry.Source , logEntry.User, logEntry.CategoryId));
}
Console.WriteLine("Press Any Key to Exit...");
Console.ReadKey();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment