Hi @Sakthi ,
According to my research and testing, you can try to use the following code to get SharePoint audit logs:
try
{
using(SPSite site = new SPSite("https://SiteURL.com")) {
using(SPWeb web = site.OpenWeb()) {
SPAuditQuery query = new SPAuditQuery(site);
//query.RestrictToList(list);
//query.RestrictToListItem(ListItem);
query.AddEventRestriction(SPAuditEventType.View);
query.AddEventRestriction(SPAuditEventType.Search);
query.SetRangeStart(DateTime.Now.AddHours(-1));
query.SetRangeEnd(DateTime.Now);
SPAuditEntryCollection auditCol = web.Audit.GetEntries(query);
foreach(SPAuditEntry audit in auditCol) {
string docName = audit.DocLocation;
string userID = audit.UserId;
string ItemID = Convert.ToString(audit.ItemId);
string ItemType = Convert.ToString(audit.ItemType);
string EventType = Convert.ToString(audit.Event);
string OccuredDate = audit.Occurred;
}
}
}
} catch (Exception ex) {
//Catch error in to ULS log.
}
More information for reference:
Retrieve Sharepoint Audit Logs Programmatically Using C# Server-Side Object Model
SharePoint Online Audit Logs
Hope it can help you. Thanks for your understanding.
Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.