The easiest way to support registration from VSTO AddIn

I wrote a simple MS Word AddIn in Visual Studio 2013, and I would like to be able to log events, preferably in the Windows Event Viewer. It seems that I will encounter resolution problems if I try to create a new event source for my application. I am looking for the simplest approach that allows me to register events from my VSTO, which does not violate the rules of good programming.

Option 1: Use VSTO 4.0 Source

I can see from the MSDN documentation on the Event Log for Office Solutions , which:

You can use the Event Viewer on Windows to view exception messages that will be caught by the Visual Studio Tools for Office environment when you install or uninstall Office solutions. You can use these messages from the event logger to solve installation and deployment problems.

These events are logged using the VSTO 4.0 source. Can I use the VSTO 4.0 source to log errors from my own AddIn, or is this considered bad practice?

Edit:. The answer from Yevgeny Astafyev and what I read in other places seems to be a bad approach, since the source of VSTO 4.0 should only deal with the management of AddIns, and not AddIns themselves. Answers to other questions also did not recommend the use of "general" sources.

Option 2. Create a boot application that will be included in the installer

Alternatively, I could include a simple boot application that creates the source during installation, but I don’t see how to add my own prerequisites using the installation settings in Properties> Publish to Visual Studio. It can be done? Is there any other way to do this? I would prefer not to create InstallShield Windows Installer , since in any other case, the default installer works well for my purposes. Overkill seems to create a complete installer just to get an event log.

Edit: It still seems that there is no easy way to do this, although it is not too difficult to create an installer by following the related instructions.

Option 3: use the registration framework and enter the file

The third option is to use log4net or a similar logging structure and configure File Appender to write to the file.

Initially, I didn’t really want to implement file logging, because: a) My application was not logged very often, and b) I wanted to avoid having the log files scattered in different places that are difficult to find.

Edit: This is the option I made so far, since it required the smallest configuration and is adaptable if my registration requirements change in the future.

+5
source share
2 answers

It is relatively simple to configure log4net to handle simple (or complex) logging requirements. The log4net Tutorial in CodeProject provides a useful link.

  • Install log4net into your project using the NuGet package manager or the package manager console if you prefer.
  • Add the App.config file if you do not already have one (right-click the project, select Add> New Item ... and select Application Configuration File .> From Visual C # Elements).
  • Edit the App.config file to look something like this (replacing MyAddIn accordingly in the <file /> ):

     <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" /> </configSections> <log4net> <root> <level value="ALL"/> <appender-ref ref="RollingFileAppender"/> </root> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="${APPDATA}\My AddIn\MyAddin-log.txt" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="5" /> <maximumFileSize value="5MB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %level %logger - %message%newline" /> </layout> </appender> </log4net> </configuration> 

    Log files will be saved in the AppData strong> user folder ( %appdata% ), storing up to 5 log files that do not exceed 5 MB.

  • Add the following line to ThisAddIn_Startup() in ThisAddIn.cs (you need to add the directive using log4net; ):

     log4net.Config.XmlConfigurator.Configure(); 
  • Add the following statement to any class that should perform logging:

     private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
  • Events can then be logged using log.Info , log.Warn , log.Error , log.Fatal and other methods provided by log4net.

You may receive schema warning messages because log4net does not include a schema, and Visual Studio cannot then check the configuration section. In this case, download the schema file from http://csharptest.net/downloads/schema/log4net.xsd to an accessible location. The location C:\Program Files\Microsoft Visual Studio 10.0\Xml\Schemas was suggested elsewhere.

Select the App.config in Visual Studio and click in the editor. You should get an XML menu item. From there, select Schemas ... and add the log4net.xsd file from where you saved it.

Note that you can also use log4net for log to Event Viewer , but you will encounter the same resolution issues raised in the original question.

+10
source

You need to distinguish between VSTO exceptions that may be caught when installing or uninstalling software and additional events handled in your code. In the Event Log for Office Solutions , this article explains how to remove Visual Studio Tools for Office exceptions when installing or uninstalling Office solutions.

If you need to log add-in events, you can use any components based on .Net and BCL. For example, see What is the best way to record event log entries? .

+1
source

Source: https://habr.com/ru/post/1213456/


All Articles