Friday, August 23, 2024

Sitecore Custom Audit Logs Generation & move into NoSQL MongoDB Part - 2

Hi All,

As I said in my last blog post, we store our own audit logs in NoSQL MongoDB. I resumed my inquiry and created the code that appears below:

MongoExportUtil.cs

 

I tried that code first with some sample files, and it worked well. However, when I tried it with actual log files, I ran into trouble once more. This function threw an error while reading the current log file because it was being utilized by the Sitecore instance, which means the file is still alive. I therefore approach this differently. I developed a PowerShell script that can transfer and relocate all files from one location—the custom audit logs location—to another. Next, I carry out the mongoexport task, which goes off without a hitch, but hold on. We may lose some content because we are still adding audit logs, even though I duplicated the active audit log file. Therefore, the response is no, you shouldn't be concerned. If we repeat the procedure in the future, it will retrieve the updated data and update the particular record rather than creating a new one because it is updating based on the file name. Currently, it will copy the information and export it to MongoDB. An existing file will update the current record in the MongoDB record if there are any content updates.

Here below the PowerShell code:

Move Audit Logs.ps1

Final Config file:

z.Audit.config

Everything is now prepared, but how do we operate those things? In order to create custom buttons in Sitecore, I followed the blogs below: one to launch a PowerShell script that transfers log files between folders, and another to export text files (.txt) into MongoDB and remove the log files from the system because we don't want to keep the text file after exporting it to MongoDB. We can also solve the space problem by doing this.






You can follow the blogs below to create buttons; let me know if you run into any difficulties.

https://community.sitecore.com/community/en?id=community_blog&sys_id=71e267211bc370d0b8954371b24bcbdb

https://www.youtube.com/watch?v=r94VxfuGlyM


Reference code: https://github.com/gaurarun777/CustomAuditLogs/tree/main

I hope you enjoy this Sitecore blog. Stay tuned for more Sitecore related articles.

Till that happy Sitecoring :)

Please leave your comments or share this article if it’s useful for you.