Created
October 9, 2017 17:34
-
-
Save deanebarker/42ece7efebe1511866828de51006231e to your computer and use it in GitHub Desktop.
A library for master logging of Episerver mirroring events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EPiServer.Enterprise; | |
using EPiServer.Enterprise.Mirroring; | |
using EPiServer.Framework; | |
using EPiServer.Framework.Initialization; | |
using EPiServer.MirroringService; | |
using EPiServer.MirroringService.Common; | |
using EPiServer.MirroringService.MirroringMonitoring; | |
using EPiServer.MirroringService.MirroringTransferProtocol.Common; | |
using System; | |
using System.Linq; | |
using System.Configuration; | |
using System.Data.SqlClient; | |
using System.Net; | |
using System.Threading.Tasks; | |
using EPiServer.ServiceLocation; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.Core.Transfer; | |
namespace Arris.MirrorLogging | |
{ | |
[InitializableModule] | |
[ModuleDependency(typeof(ServiceContainerInitialization))] | |
public class EventHandlers : IInitializableModule | |
{ | |
public void Initialize(InitializationEngine context) | |
{ | |
var repo = ServiceLocator.Current.GetInstance<IContentRepository>(); | |
DataExporter.ContentExporting += (DataExporter sender, ContentExportingEventArgs e) => | |
{ | |
var eventName = "DataExporter.ContentExporting"; | |
var content = repo.Get<IContent>(e.ContentLink); | |
Write(eventName, null, "Content: " + content.Name); | |
}; | |
DataExporter.ContentExported += (DataExporter sender, ContentExportedEventArgs e) => | |
{ | |
var eventName = "DataExporter.ContentExported"; | |
var content = repo.Get<IContent>(e.ContentLink); | |
Write(eventName, null, "Content: " + content.Name); | |
}; | |
MirroringManager.ExecutingMirror += (object sender, EventArgs e) => | |
{ | |
var eventName = "MirroringManager.ExecutingMirror"; | |
var mirroringData = (MirroringData)sender; | |
Write(eventName, mirroringData.Name); | |
}; | |
MirroringManager.ExecutedMirror += (object sender, EventArgs e) => | |
{ | |
var eventName = "MirroringManager.ExecutedMirror"; | |
var mirroringData = (MirroringData)sender; | |
Write(eventName, mirroringData.Name); | |
}; | |
MirroringManager.FailedExecuteMirror += (object sender, FailedExecuteMirroringEventArgs e) => | |
{ | |
var eventName = "MirroringManager.FailedExecuteMirror"; | |
var mirroringData = (MirroringData)sender; | |
Write(eventName, mirroringData.Name); | |
}; | |
MirroringEvents.SourceStartingJob += (object source, MirroringSourceInitializeEventArgs e) => | |
{ | |
var eventName = "MirroringEvents.SourceStartingJob"; | |
Write(eventName, e.MirroringData.Name); | |
}; | |
MirroringEvents.SourceJobCompleted += (object source, MirroringCompletedEventArgs e) => | |
{ | |
var eventName = "MirroringEvents.SourceJobCompleted"; | |
Write(eventName, null); | |
}; | |
MirroringEvents.TargetStartingJob += (object source, MirroringTargetInitializeEventArgs e) => | |
{ | |
var eventName = "MirroringEvents.TargetStartingJob"; | |
Write(eventName, e.MirroringInitializeData.Name); | |
}; | |
MirroringEvents.TargetJobCompleted += (object source, MirroringCompletedEventArgs e) => | |
{ | |
var eventName = "MirroringEvents.TargetJobCompleted"; | |
Write(eventName, null); | |
}; | |
DataImporter.ContentImporting += (DataImporter dataImported, ContentImportingEventArgs e) => | |
{ | |
var eventName = "DataImporter.ContentImporting"; | |
Write(eventName, null); | |
}; | |
DataImporter.ContentImported += (DataImporter dataImported, ContentImportedEventArgs e) => | |
{ | |
var eventName = "DataImporter.ContentImported"; | |
var content = repo.Get<IContent>(e.ContentLink); | |
Write(eventName, null, "Content: " + content.Name); | |
}; | |
} | |
private static void Write(string eventString, string job, params string[] messages) | |
{ | |
if(ConfigurationManager.AppSettings["mirroringLogKey"] == null) | |
{ | |
return; | |
} | |
if (ConfigurationManager.ConnectionStrings["mirroringLog"] == null) | |
{ | |
return; | |
} | |
if(!messages.Any()) | |
{ | |
// No messages mean the event simply occured | |
messages = new string[] { string.Empty }; | |
} | |
var key = ConfigurationManager.AppSettings["mirroringLogKey"]; | |
var connectionString = ConfigurationManager.ConnectionStrings["mirroringLog"].ToString(); | |
var className = eventString.Split('.').First(); | |
var eventName = eventString.Split('.').Last(); | |
using (var connection = new SqlConnection(connectionString)) | |
{ | |
connection.Open(); | |
foreach (var message in messages) | |
{ | |
var command = new SqlCommand("INSERT INTO Log (source, class, event, job, message) VALUES (@source, @class, @event, @job, @message)", connection); | |
command.Parameters.AddWithValue("source", key); | |
command.Parameters.AddWithValue("class", className); | |
command.Parameters.AddWithValue("event", eventName); | |
command.Parameters.AddWithValue("job", job ?? string.Empty); | |
command.Parameters.AddWithValue("message", message); | |
command.ExecuteNonQuery(); | |
} | |
} | |
} | |
public void Uninitialize(InitializationEngine context) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment