By default, logging is disabled when rendering documents. You can log to the console or a file.
To log, use the ILogger interface. It defines the methods required to instantiate and release the output file stream. Also, use the following classes:
The ConsoleLogger class defines the methods required to log to the console.
The FileLogger class defines the methods required to log to a file.
The log contains the following three types of messages:
Error means unrecoverable exceptions
Warning means recoverable/expected exceptions
Trace means general information
Logging to a file
The following code snippet shows how to log to a file using the FileLogger class:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Logging;usingGroupDocs.Viewer.Options;// ...// Create logger and specify the output file.FileLoggerfileLogger=newFileLogger("output.log");// Create ViewerSettings and specify FileLogger.ViewerSettingsviewerSettings=newViewerSettings(fileLogger);// Render a document.using(Viewerviewer=newViewer("sample.docx",viewerSettings)){ViewOptionsviewOptions=HtmlViewOptions.ForEmbeddedResources("result.html");viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.LoggingImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())' Create logger and specify the output file.
DimfileLoggerAsFileLogger=NewFileLogger("output.log")' Create ViewerSettings and specify FileLogger.
DimviewerSettingsAsViewerSettings=NewViewerSettings(fileLogger)' Render a document.
UsingviewerAsViewer=NewViewer("sample.docx",viewerSettings)DimviewOptionsAsViewOptions=HtmlViewOptions.ForEmbeddedResources("result.html")viewer.View(viewOptions)EndUsingEndSubEndModule
The following image shows the output.log file:
Logging to the console
The following code snippet shows how to log to the console using the ConsoleLogger class:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Logging;usingGroupDocs.Viewer.Options;// ...// Create console logger.ConsoleLoggerconsoleLogger=newConsoleLogger();// Create ViewerSettings and specify FileLogger.ViewerSettingsviewerSettings=newViewerSettings(consoleLogger);// Render a document.using(Viewerviewer=newViewer("sample.docx",viewerSettings)){ViewOptionsviewOptions=HtmlViewOptions.ForEmbeddedResources();viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.LoggingImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())' Create console logger.
DimconsoleLoggerAsConsoleLogger=NewConsoleLogger()' Create ViewerSettings and specify FileLogger.
DimviewerSettingsAsViewerSettings=NewViewerSettings(consoleLogger)' Render a document.
UsingviewerAsViewer=NewViewer("sample.docx",viewerSettings)DimviewOptionsAsViewOptions=HtmlViewOptions.ForEmbeddedResources()viewer.View(viewOptions)EndUsingEndSubEndModule
The following image shows a sample console output:
Implementing custom logger
To create a logger, implement the ILogger interface:
For trace messages, implement the public void Trace(string message) method.
For warning messages, implement the public void Warning(string message) method.
For error messages, implement the public void Error(string message) method.
The following code snippet shows how to implement a simple file logger:
usingSystem;usingSystem.IO;usingGroupDocs.Viewer;usingGroupDocs.Viewer.Logging;usingGroupDocs.Viewer.Options;// ...// Create logger and specify the output file.CustomLoggercustomLogger=newCustomLogger("output.log");// Create ViewerSettings and specify CustomLogger.ViewerSettingsviewerSettings=newViewerSettings(customLogger);// Render a document.using(Viewerviewer=newViewer("sample.docx",viewerSettings)){ViewOptionsviewOptions=HtmlViewOptions.ForEmbeddedResources("result.html");viewer.View(viewOptions);}/// <summary>/// Writes log messages to a file./// </summary>publicclassCustomLogger:ILogger{privatereadonlystring_fileName;privateCustomLogger(){}/// <summary>/// Create logger to a file./// </summary>/// <param name="fileName">Full file name with path</param>publicCustomLogger(stringfileName){_fileName=fileName;}/// <summary>/// Writes trace message to the 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){if(message==null)thrownewArgumentNullException(nameof(message));using(StreamWriterwr=newStreamWriter(_fileName,true)){wr.WriteLine($"[TRACE] {message}");}}/// <summary>/// Writes warning message to the file;/// Warning log messages provide information about the unexpected and recoverable events 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){if(message==null)thrownewArgumentNullException(nameof(message));using(StreamWriterwr=newStreamWriter(_fileName,true)){wr.WriteLine($"[WARN] {message}");}}/// <summary>/// Writes an error message to the 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){if(message==null)thrownewArgumentNullException(nameof(message));if(exception==null)thrownewArgumentNullException(nameof(exception));using(StreamWriterwr=newStreamWriter(_fileName,true)){wr.WriteLine($"[ERROR] {message}, exception: {exception}");}}}
ImportsSystemImportsSystem.IOImportsGroupDocs.ViewerImportsGroupDocs.Viewer.LoggingImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())' Create logger and specify the output file.
DimcustomLoggerAsCustomLogger=NewCustomLogger("output.log")' Create ViewerSettings and specify CustomLogger.
DimviewerSettingsAsViewerSettings=NewViewerSettings(customLogger)' Render a document.
UsingviewerAsViewer=NewViewer("sample.docx",viewerSettings)DimviewOptionsAsViewOptions=HtmlViewOptions.ForEmbeddedResources("result.html")viewer.View(viewOptions)EndUsingEndSub' <summary>
' Writes log messages to a file.
' </summary>
PublicClassCustomLoggerImplementsILoggerPrivateReadOnly_fileNameAsStringPrivateSubNew()EndSub''' <summary>
''' Create logger to a file.
''' </summary>
''' <param name="fileName">Full file name with path</param>
PublicSubNew(fileNameAsString)_fileName=fileNameEndSub''' <summary>
''' Writes warning message to the file;
''' Warning log messages provide information about the unexpected and recoverable events in application flow.
''' </summary>
''' <param name="message">The warning message.</param>
''' <exception cref="System.ArgumentNullException">Thrown when <paramref name="message"/> is null.</exception>
PublicSubILogger_Warning(messageAsString)ImplementsILogger.WarningIfmessageIsNothingThenThrowNewArgumentNullException(NameOf(message))EndIfUsingwrAsNewStreamWriter(_fileName,True)wr.WriteLine($"[WARN] {message}")EndUsingEndSub''' <summary>
''' Writes trace message to the 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>
PublicSubILogger_Trace(messageAsString)ImplementsILogger.TraceIfmessageIsNothingThenThrowNewArgumentNullException(NameOf(message))EndIfUsingwrAsNewStreamWriter(_fileName,True)wr.WriteLine($"[TRACE] {message}")EndUsingEndSub''' <summary>
''' Writes an error message to the 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>
PublicSub[Error](messageAsString,exceptionAsException)ImplementsILogger.[Error]IfmessageIsNothingThenThrowNewArgumentNullException(NameOf(message))EndIfIfexceptionIsNothingThenThrowNewArgumentNullException(NameOf(exception))EndIfUsingwrAsNewStreamWriter(_fileName,True)wr.WriteLine($"[ERROR] {message}, exception: {exception}")EndUsingEndSubEndClassEndModule
The following image shows the output.log file:
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.