【问题标题】:VS Publish, Web Deploy - create Event SourceVS Publish, Web Deploy - 创建事件源
【发布时间】:2016-02-18 10:55:38
【问题描述】:

当我通过 IIS 导入应用程序时,我想创建一个新的事件日志事件源来记录我的 webAPI 应用程序。

我将我的应用程序发布到 VS2015 中的 web 部署文件夹 (zip) 文件并从中导入。

我找到了这段代码来创建事件源:

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

我可以将它放在 EventSource.ps1 文件中,当我从提示符运行它时,它会执行我想要的操作。

如何在 IIS 导入应用程序 过程中执行此操作?

我尝试过使用 .pubxml 文件,但使用/覆盖/调用它的元素让我感到困惑 - 我尝试过 AfterAddIisSettingAndFileContentsToSourceManifestPipelineDependsOn

    <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>

我宁愿它是通过 IIS 导入应用程序完成的,作为一击过程,而不是:

  1. 导入应用程序
  2. 运行 powershell

因为它将由非技术用户导入。

非常感谢您抽出宝贵时间提供帮助!

【问题讨论】:

    标签: powershell iis webdeploy


    【解决方案1】:

    当应用程序导入 IIS 时,您可以使用 &lt;projectName&gt;.wpp.targets 文件启动自定义命令。

    例如,如果您的项目名称是 MyApp,则将名为 MyApp.wpp.targets 的文件添加到项目的根级别。

    我测试并验证了这种方法在使用具有以下内容的目标时有效:

    <?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>
    

    需要注意的重要一点是,Visual Studio 有时会缓存 .wpp.targets 文件。我知道释放它的唯一方法是重新启动 Visual Studio。我刚刚遇到了这个问题,这让我有一段时间偏离了轨道,因为我遇到了一个我无法解决的错误。

    作为参考,这里是我用来创建压缩包的.pubxml文件:

    <?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>
    

    最后,这里有一些资源可能会有所帮助:

    【讨论】:

    • 我刚刚注意到格式阻止了 *.wpp.targets 文件的一部分出现。此问题已修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多