17

I am trying to log from my C# Custom Action using session.Log( "Hello World!" ); This does not show up in my log file when executing my msi as follows:

msiexec /i myMsi.msi /lvx myLog.log

My custom action works fine, my only problem is I do not get my logging info. The log shows my CA is getting called, just not the info from my session.Log() call.

I am using Wix 3.5, .Net 4, VS 2010, and 64-bit Windows 7. I am calling my action as follows.

<Control Id="TestConnection" Type="PushButton" X="21" Y="177" Width="100" Height="17" Text="Test Connection">
  <Publish Event="DoAction" Value="TestConnection">1</Publish>
</Control>
KnightsArmy
  • 680
  • 1
  • 6
  • 12

2 Answers2

30

Per the docs on DoAction ControlEvent, MsiProcessMessage (the API behind session.Log) cannot be used from a ControlEvent. This prevents your message from showing up in the log. If you need to log some information from a ControlEvent (especially for debugging), your best bet is a hack like changing a property's value to contain your desired log information.

Michael Urman
  • 15,737
  • 2
  • 28
  • 44
  • 1
    Also session.Message relies on the same MsiProcessMessage (and not Message as one would hope) – Jake Apr 17 '14 at 18:15
  • > On systems prior to Windows Server 2003, custom actions launched by a DoAction ControlEvent cannot send messages with MsiProcessMessage or Message Method. For more information, see Sending Messages to Windows Installer Using MsiProcessMessage. – Sebazzz Apr 16 '19 at 14:02
1

I got around this by using the unmanaged OutputDebugString:

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern void OutputDebugString(string message);

public bool IsDebugLogging { get; set; }

public void Log(string message)
{
    // Uncomment if you have access to the Session object passed into your custom action.
    // Session.Log(message);
    if (IsDebugLogging)
    {
        OutputDebugString(message);
    }
}

In my custom action, I set IsDebugLogging = true to enable OutputDebugString logging. This output can be viewed using Sysinternal's DebugView.

Polyfun
  • 9,479
  • 4
  • 31
  • 39
  • Hi, when I do this I get the error "An object reference is required for the non-static field, method, or property `Session.Log(string)`". How can this be helped? – Nelson Frew Jan 20 '20 at 19:34
  • 1
    You can comment out the Session.Log line; that is the Session object that is passed into your custom action, in this case I am only using it to attempt to log to the Session as well as the debug window. – Polyfun Jan 21 '20 at 09:58