Last active
May 31, 2019 14:36
-
-
Save oleksabor/91dc8946d1277809ec322dafb30b6d76 to your computer and use it in GitHub Desktop.
debug docfx pdf generation. Requires NLog, Topshelf.NLog, LibLog4 packages
This file contains hidden or 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 System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Topshelf; | |
using windowsService1.Logging; | |
namespace windowsService1 | |
{ | |
class Program | |
{ | |
static ILog Log = LogProvider.GetCurrentClassLogger(); | |
static void Main(string[] args) | |
{ | |
var rc = HostFactory.Run(x => //1 | |
{ | |
x.UseNLog(); | |
x.Service<ConsoleRunner>(); | |
x.RunAsNetworkService(); //6 | |
x.SetDescription("Sample WindowsService Host"); //7 | |
x.SetDisplayName("TestStuff"); //8 | |
x.SetServiceName("TestStuff"); //9 | |
}); //10 | |
var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode()); //11 | |
Environment.ExitCode = exitCode; | |
Log.Info(() => $"exit code is {exitCode}"); | |
} | |
} | |
class ConsoleRunner : ServiceControl | |
{ | |
ILog Log = LogProvider.GetCurrentClassLogger(); | |
public bool Start(HostControl hostControl) | |
{ | |
var IsOutputToStdout = true; | |
var PdfCommandName = "wkhtmltopdf"; | |
//var arguments = "--javascript-delay 3000 -q --no-outline --encoding utf-8 --user-style-sheet \"defaults/default-css.css\" \"D:/work/DirectClientDataSync/docfx_project/_site_pdf/_raw/_site_pdf/pdf/../articles/export.sample.html\" -"; | |
var arguments = "--javascript-delay 3000 -q --no-outline --encoding utf-8 --user-style-sheet \"defaults/default-css.css\" \"E:/buildagent/network/_work/10/b/_site_pdf/_raw/_site_pdf/pdf/TOC.html\" -"; | |
var TimeoutInMilliseconds = 3000; | |
try | |
{ | |
using (var stream = new MemoryStream()) | |
Run(PdfCommandName, IsOutputToStdout, arguments, TimeoutInMilliseconds, stream); | |
return true; | |
} | |
catch (Exception e) | |
{ | |
Log.FatalException($"failed to start {PdfCommandName}", e); | |
return false; | |
} | |
} | |
public int Run(string PdfCommandName, bool IsOutputToStdout, string arguments, int TimeoutInMilliseconds, Stream stream) | |
{ | |
using (var process = new Process | |
{ | |
StartInfo = new ProcessStartInfo | |
{ | |
UseShellExecute = false, | |
RedirectStandardOutput = IsOutputToStdout, | |
RedirectStandardError = IsOutputToStdout, | |
//WindowStyle = ProcessWindowStyle.Hidden, | |
FileName = PdfCommandName, | |
Arguments = arguments, | |
CreateNoWindow = true | |
} | |
}) | |
{ | |
Log.Debug($"Executing {process.StartInfo.FileName} {process.StartInfo.Arguments} {arguments}"); | |
process.Start(); | |
//string so = "", se = ""; | |
if (IsOutputToStdout) | |
{ | |
using (var standardOutput = process.StandardOutput) | |
{ | |
standardOutput.BaseStream.CopyTo(stream); | |
} | |
//so = process.StandardOutput.ReadToEnd(); | |
//se = process.StandardError.ReadToEnd(); | |
} | |
var exited = process.WaitForExit(TimeoutInMilliseconds); | |
//if (exited && IsOutputToStdout) | |
//{ | |
// so += process.StandardOutput.ReadToEnd(); | |
// se += process.StandardError.ReadToEnd(); | |
// var buf = Encoding.UTF8.GetBytes(so); | |
// Log.Debug($"has got {process.StartInfo.FileName} {arguments} output, {buf.Length}Bytes"); | |
//} | |
Log.Debug($"has got {process.StartInfo.FileName} output, stream:{stream.Length}Bytes"); | |
return process.ExitCode; | |
} | |
} | |
public bool Stop(HostControl hostControl) | |
{ | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requires NLog, Topshelf.NLog, LibLog4 packages