2

I want to create an new event log Event Source to log my webAPI app to when I Import Application through IIS.

I publish my application to a web deploy folder (zip) file in VS2015 and import from this.

I have found this code to create the event source:

if ([System.Diagnostics.EventLog]::SourceExists("myWeb.API") -eq $false) {
    [System.Diagnostics.EventLog]::CreateEventSource("myWeb.API", "Application")
}

and I can put this in a EventSource.ps1 file which does what I want when I run it from a prompt.

How can I execute this during the IIS Import Application process?

I have tried using the .pubxml file but which element to use/override/call-it-via baffles me - I've tried AfterAddIisSettingAndFileContentsToSourceManifest and PipelineDependsOn.

    <Target Name="CustomCreateEventSource">
    <Message Text="Create Event Source" Importance="high"/>
    <PropertyGroup>
        <EventSource Condition=" '$(EventSource)'=='' ">
            myWeb.API
        </EventSource>
    </PropertyGroup>
<Exec Command="powershell.exe"
-NonInteractive
-executionpolicy Unrestricted
-file &quot;$(PublishUrl)Publish\EventSource.ps1&quot; &quot;$(EventSource)&quot;" /></Target>

I'd rather it was done via IIS Import Application, as a 1-hit process, and not a:

  1. Import Application
  2. Run the powershell

because it'll be imported by not-necessarily technical users.

Many thanks for taking the time to assist!

ninety
  • 23
  • 2

1 Answers1

1

You can use the <projectName>.wpp.targets file to kick off a custom command when the application is imported into IIS.

For example, if your project name is MyApp, add a file called MyApp.wpp.targets to the root level of the project.

I tested and verified that this approach works using a targets with the following content:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="CustomTask" AfterTargets="AddIisSettingAndFileContentsToSourceManifest">
    <ItemGroup>
      <MsDeploySourceManifest Include="runCommand">
        <!-- specify InputFormat None (see http://stackoverflow.com/questions/4238192/running-powershell-from-msdeploy-runcommand-does-not-exit) -->
        <Path>powershell.exe -ExecutionPolicy bypass -NoLogo -inputformat none -NonInteractive -Command .'$(_MSDeployDirPath_FullPath)\Deploy\createEventLog.ps1'</Path>
      </MsDeploySourceManifest>
    </ItemGroup>
  </Target>
</Project>

One important thing to note is that Visual Studio will sometimes cache the .wpp.targets file. The only way I know to release it is to restart Visual Studio. I just experienced this issue which threw me off track for a while, because I was getting an error that I couldn't make go away.

For reference, here is the .pubxml file I used to create the package Zip:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>Package</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <DesktopBuildPackageLocation>c:\temp\IISImportTestPkg.zip</DesktopBuildPackageLocation>
    <PackageAsSingleFile>true</PackageAsSingleFile>
    <DeployIisAppPath>Default Web Site/IISImportTest</DeployIisAppPath>
    <PublishDatabaseSettings>
      <Objects xmlns="" />
    </PublishDatabaseSettings>
  </PropertyGroup>
</Project>

Lastly, here are a couple of resources that may help:

Joseph Gabriel
  • 8,339
  • 3
  • 39
  • 53