To build your custom logger you have to implemnt ILogger interface that defines the methods that are used to perform logging.
There are 3 types of messages in a log file:
Error - for unrecoverable exceptions
Warning - for recoverable/expected exceptions
Trace - for general information
Write logs to a file
The following example shows how to use FileLogger class to write logs to output.log file.
// Create logger and specify the output fileFileLoggerfileLogger=newFileLogger("output.log");// Create ViewerSettings and specify FileLoggerViewerSettingsviewerSettings=newViewerSettings(fileLogger);using(Viewerviewer=newViewer("sample.docx",viewerSettings)){ViewOptionsviewOptions=HtmlViewOptions.ForEmbeddedResources();viewer.View(viewOptions);}
The sample contents of the output.log file:
[TRACE] Rendering into HTML with embedded resources.
[TRACE] Detecting file type by extension.
[TRACE] File type detected successfully: Microsoft Word Open XML Document.
[TRACE] Opening document.
[TRACE] Document is loaded.
[TRACE] Applying options.
[TRACE] Converting page 1.
[TRACE] Page 1 conversion completed.
[TRACE] Converting page 2.
[TRACE] Page 2 conversion completed.
[TRACE] Releasing resources.
Write logs to a console
The following example shows how to use ConsoleLogger class and write logs to console.
// Create console loggerConsoleLoggerconsoleLogger=newConsoleLogger();// Create ViewerSettings and specify FileLoggerViewerSettingsviewerSettings=newViewerSettings(consoleLogger);using(Viewerviewer=newViewer("sample.docx",viewerSettings)){ViewOptionsviewOptions=HtmlViewOptions.ForEmbeddedResources();viewer.View(viewOptions);}
The following image demonstrates sample console output:
For trace messages - implement public void Trace(string message) method For warning messages - implement public void Warning(string message) method For error messages - implement public void Error(string message) method
The following example demonstrates how to implement custom logger:
// Create logger and specify the output fileMyFileLoggerlogger=newMyFileLogger("log.txt");// Create ViewerSettings and pass loggerViewerSettingsviewerSettings=newViewerSettings(logger);using(Viewerviewer=newViewer("sample.docx",viewerSettings)){ViewOptionsviewOptions=HtmlViewOptions.ForEmbeddedResources();viewer.View(viewOptions);}/// <summary>/// Writes log messages to the file./// </summary>publicclassMyFileLogger:ILogger{privatereadonlystring_fileName;/// <summary>/// Create logger to file./// </summary>/// <param name="fileName">Full file name with path</param>publicMyFileLogger(stringfileName){_fileName=fileName;}/// <summary>/// Writes trace message to file./// Trace log messages provide generally useful information about application flow./// </summary>/// <param name="message">The trace message.</param>/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="message"/> is null.</exception>publicvoidTrace(stringmessage)=>File.AppendAllText(_fileName,$"[TRACE] {message}{Environment.NewLine}");/// <summary>/// Writes warning message to file./// Warning log messages provide information about the unexpected and recoverable event in application flow./// </summary>/// <param name="message">The warning message.</param>/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="message"/> is null.</exception>publicvoidWarning(stringmessage)=>File.AppendAllText(_fileName,$"[WARN] {message}{Environment.NewLine}");/// <summary>/// Writes an error message to file./// Error log messages provide information about unrecoverable events in application flow./// </summary>/// <param name="message">The error message.</param>/// <param name="exception">The exception.</param>/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="message"/> is null.</exception>/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="exception"/> is null.</exception>publicvoidError(stringmessage,Exceptionexception)=>File.AppendAllText(_fileName,$"[ERROR] {message}, exception: {exception}{Environment.NewLine}");}
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.