Added SessionWriterTraceListener.cs class

This commit is contained in:
S T Chan 2014-01-06 11:14:19 -05:00
parent 041ff0d11d
commit 000216e993
4 changed files with 119 additions and 17 deletions

View File

@ -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;
}
/*
/// <summary>
/// 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

View File

@ -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,12 +44,13 @@ 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,
@ -59,9 +60,12 @@ namespace PdfScribeInstallCustomAction
else
printerInstalled = ActionResult.Failure;
installTraceListener.Flush();
installTraceListener.Close();
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

View File

@ -82,6 +82,7 @@
<ItemGroup>
<Compile Include="CustomAction.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SessionWriterTraceListener.cs" />
<Content Include="CustomAction.config" />
</ItemGroup>
<ItemGroup>

View File

@ -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
/// <summary>
/// Releases resources held by the listener -
/// Note will not automatically flush and write
/// trace data to the install session -
/// call CloseAndWriteLog() before disposing
/// </summary>
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
/// <summary>
/// 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.
/// </summary>
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();
}
}
}
}