From 000216e99343360753b96d9c9a3370677accdbcd Mon Sep 17 00:00:00 2001 From: S T Chan Date: Mon, 6 Jan 2014 11:14:19 -0500 Subject: [PATCH] Added SessionWriterTraceListener.cs class --- PdfScribeCore/PdfScribeInstaller.cs | 6 +- PdfScribeInstallCustomAction/CustomAction.cs | 34 ++++--- .../PdfScribeInstallCustomAction.csproj | 1 + .../SessionWriterTraceListener.cs | 95 +++++++++++++++++++ 4 files changed, 119 insertions(+), 17 deletions(-) create mode 100644 PdfScribeInstallCustomAction/SessionWriterTraceListener.cs diff --git a/PdfScribeCore/PdfScribeInstaller.cs b/PdfScribeCore/PdfScribeInstaller.cs index e4aa529..7c299b8 100644 --- a/PdfScribeCore/PdfScribeInstaller.cs +++ b/PdfScribeCore/PdfScribeInstaller.cs @@ -98,14 +98,16 @@ namespace PdfScribeCore this.logEventSource.Listeners.Add(additionalListener); } + #region Constructors public PdfScribeInstaller() { this.logEventSource = new TraceSource(logEventSourceNameDefault); - this.logEventSource.Switch = new SourceSwitch("PdfScribeCoreSwitch"); + this.logEventSource.Switch = new SourceSwitch("PdfScribeCoreAll"); this.logEventSource.Switch.Level = SourceLevels.All; } + /* /// /// This override sets the /// trace source to a specific name @@ -124,7 +126,7 @@ namespace PdfScribeCore this.logEventSource.Switch = new SourceSwitch("PdfScribeCoreSwitch"); this.logEventSource.Switch.Level = SourceLevels.All; } - + */ #endregion #region Port operations diff --git a/PdfScribeInstallCustomAction/CustomAction.cs b/PdfScribeInstallCustomAction/CustomAction.cs index 3974421..469ce29 100644 --- a/PdfScribeInstallCustomAction/CustomAction.cs +++ b/PdfScribeInstallCustomAction/CustomAction.cs @@ -24,7 +24,7 @@ namespace PdfScribeInstallCustomAction { ActionResult resultCode; TextWriterTraceListener installTraceListener = new TextWriterTraceListener("C:\\testout.txt"); - PdfScribeInstaller installer = new PdfScribeInstaller(traceSourceName); + PdfScribeInstaller installer = new PdfScribeInstaller(); installer.AddTraceListener(installTraceListener); if (installer.IsPdfScribePrinterInstalled()) resultCode = ActionResult.Success; @@ -44,24 +44,28 @@ namespace PdfScribeInstallCustomAction String outputCommand = session.CustomActionData["OutputCommand"]; String outputCommandArguments = session.CustomActionData["OutputCommandArguments"]; - TextWriterTraceListener installTraceListener = new TextWriterTraceListener("C:\\testout.txt"); - installTraceListener.TraceOutputOptions = TraceOptions.Timestamp; - - PdfScribeInstaller installer = new PdfScribeInstaller(traceSourceName); + SessionLogWriterTraceListener installTraceListener = new SessionLogWriterTraceListener(session); + installTraceListener.TraceOutputOptions = TraceOptions.DateTime; + PdfScribeInstaller installer = new PdfScribeInstaller(); installer.AddTraceListener(installTraceListener); + try + { - if (installer.InstallPdfScribePrinter(driverSourceDirectory, - outputCommand, - outputCommandArguments)) - printerInstalled = ActionResult.Success; - else - printerInstalled = ActionResult.Failure; - - installTraceListener.Flush(); - installTraceListener.Close(); + if (installer.InstallPdfScribePrinter(driverSourceDirectory, + outputCommand, + outputCommandArguments)) + printerInstalled = ActionResult.Success; + else + printerInstalled = ActionResult.Failure; + installTraceListener.CloseAndWriteLog(); + } + finally + { + if (installTraceListener != null) installTraceListener.Dispose(); + } return printerInstalled; } @@ -71,7 +75,7 @@ namespace PdfScribeInstallCustomAction { ActionResult printerUninstalled; - PdfScribeInstaller installer = new PdfScribeInstaller(traceSourceName); + PdfScribeInstaller installer = new PdfScribeInstaller(); if (installer.UninstallPdfScribePrinter()) printerUninstalled = ActionResult.Success; else diff --git a/PdfScribeInstallCustomAction/PdfScribeInstallCustomAction.csproj b/PdfScribeInstallCustomAction/PdfScribeInstallCustomAction.csproj index 92ce26b..c524050 100644 --- a/PdfScribeInstallCustomAction/PdfScribeInstallCustomAction.csproj +++ b/PdfScribeInstallCustomAction/PdfScribeInstallCustomAction.csproj @@ -82,6 +82,7 @@ + diff --git a/PdfScribeInstallCustomAction/SessionWriterTraceListener.cs b/PdfScribeInstallCustomAction/SessionWriterTraceListener.cs new file mode 100644 index 0000000..5ae52f7 --- /dev/null +++ b/PdfScribeInstallCustomAction/SessionWriterTraceListener.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; + +using Microsoft.Deployment.WindowsInstaller; + +namespace PdfScribeInstallCustomAction +{ + public class SessionLogWriterTraceListener : TextWriterTraceListener , IDisposable + { + + protected MemoryStream listenerStream; + protected Session installSession; + private bool isDisposed; + + public SessionLogWriterTraceListener(Session session) + : base() + { + this.listenerStream = new MemoryStream(); + this.Writer = new StreamWriter(this.listenerStream); + this.installSession = session; + } + + #region IDisposable impelementation + + /// + /// Releases resources held by the listener - + /// Note will not automatically flush and write + /// trace data to the install session - + /// call CloseAndWriteLog() before disposing + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dipose(bool disposing) + { + if (!this.isDisposed) + { + if (disposing) + { + if (this.Writer != null) + { + this.Writer.Close(); + this.Writer.Dispose(); + this.Writer = null; + } + if (this.listenerStream != null) + { + this.listenerStream.Close(); + this.listenerStream.Dispose(); + this.listenerStream = null; + } + if (this.installSession != null) + this.installSession = null; + } + this.isDisposed = true; + } + + base.Dispose(disposing); + } + + #endregion + + /// + /// Closes the listener and writes accumulated + /// trace data to the install session's log (Session.Log) + /// The listener will no longer be usable after calling + /// this method, and should be disposed of. + /// + public void CloseAndWriteLog() + { + if (this.listenerStream != null && + this.installSession != null) + { + this.Flush(); + this.Close(); + if (this.listenerStream.Length > 0) + { + listenerStream.Position = 0; + using (StreamReader listenerStreamReader = new StreamReader(this.listenerStream)) + { + this.installSession.Log(listenerStreamReader.ReadToEnd()); + } + } + this.Dispose(); + } + } + } +}