1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
using System; using System.Text; using Microsoft.Crm.Callout; using System.Threading; using System.IO; using System.Diagnostics;
namespace PlainConcepts.CRMTools.Logging { public class LogCallout : CrmCalloutBase {
public override void PostAssign(CalloutUserContext userContext, CalloutEntityContext entityContext, string preImageEntityXml, string postImageEntityXml) { LogInfo log = new LogInfo(OperationsType.Assign, userContext.UserId.ToString(), preImageEntityXml, postImageEntityXml);
ThreadPool.QueueUserWorkItem(new WaitCallback(WriteToFile.Write), log); }
public override void PostCreate(CalloutUserContext userContext, CalloutEntityContext entityContext, string postImageEntityXml) { LogInfo log = new LogInfo(OperationsType.Create, userContext.UserId.ToString(), postImageEntityXml, null);
ThreadPool.QueueUserWorkItem(new WaitCallback(WriteToFile.Write), log); }
public override void PostDelete(CalloutUserContext userContext, CalloutEntityContext entityContext, string preImageEntityXml) { LogInfo log = new LogInfo(OperationsType.Delete, userContext.UserId.ToString(), preImageEntityXml, null);
ThreadPool.QueueUserWorkItem(new WaitCallback(WriteToFile.Write), log); }
public override void PostDeliver(CalloutUserContext userContext, CalloutEntityContext entityContext, string postImageEntityXml) { LogInfo log = new LogInfo(OperationsType.Deliver, userContext.UserId.ToString(), postImageEntityXml, null);
ThreadPool.QueueUserWorkItem(new WaitCallback(WriteToFile.Write), log); }
public override void PostMerge(CalloutUserContext userContext, CalloutEntityContext entityContext, string preImageMasterEntityXml, string preImageSubordinateEntityXml, string postImageMasterEntityXml) { LogInfo log = new LogInfo(OperationsType.Merge, userContext.UserId.ToString(), preImageMasterEntityXml + " " + preImageSubordinateEntityXml, postImageMasterEntityXml);
ThreadPool.QueueUserWorkItem(new WaitCallback(WriteToFile.Write), log); }
public override void PostSetState(CalloutUserContext userContext, CalloutEntityContext entityContext, string preImageEntityXml, string postImageEntityXml) { LogInfo log = new LogInfo(OperationsType.SetState, userContext.UserId.ToString(), preImageEntityXml, postImageEntityXml);
ThreadPool.QueueUserWorkItem(new WaitCallback(WriteToFile.Write), log); }
public override void PostUpdate(CalloutUserContext userContext, CalloutEntityContext entityContext, string preImageEntityXml, string postImageEntityXml) { LogInfo log = new LogInfo(OperationsType.Update, userContext.UserId.ToString(), preImageEntityXml, postImageEntityXml);
ThreadPool.QueueUserWorkItem(new WaitCallback(WriteToFile.Write), log); }
}
class WriteToFile { static object sync;
public static void Write(Object logInfo) { Monitor.Enter(sync); String path = DateTime.Now.Month + "-" + DateTime.Now.Day + "_MSCRM.log"; StreamWriter sw; try { sw = File.AppendText(path); sw.WriteLine(logInfo.ToString()); sw.Close(); } catch (IOException e) { if (!System.Diagnostics.EventLog.SourceExists("CRMDataMonitor")) System.Diagnostics.EventLog.CreateEventSource( "CRMDataMonitor", "Application");
EventLog.WriteEntry("CRMDataMonitor", "Error trying to write the log: \n" + e.Message + "\n" + e.StackTrace); } finally { Monitor.Exit(sync); } } }
class LogInfo { public LogInfo(OperationsType opType, String userId, String preImage, String postImage) { this.opType = opType; this.userId = userId; this.preImage = preImage; this.postImage = postImage; }
public DateTime time = DateTime.Now; public OperationsType opType; public String userId; public String preImage; public String postImage;
public override string ToString() { return time + "\t" + opType + "\t" + userId + "\t" + preImage + "\t" + ((postImage != null) ? postImage : ""); }
}
enum OperationsType { Assign, Create, Delete, Deliver, Merge, SetState, Update } } |