简体   繁体   English

在.csproj 文件中手动添加 Elmah 和 ApplicationInsights

[英]Manually adding Elmah & ApplicationInsights in .csproj file

I created a multi-project template and connected it with a wizard.我创建了一个多项目模板并将其与向导连接起来。 Everything is working fine, and I can restore packages using update-package -reinstall in the package console manager.一切正常,我可以在 package 控制台管理器中使用update-package -reinstall来恢复包。

I'm trying to add Elmah & ApplicationInsights with a condition in the .csproj file of the template, so that it'll add Elmah or ApplicationInsights based on the answer to the wizard.我正在尝试在模板的.csproj文件中添加带有条件的 Elmah 和 ApplicationInsights,以便它根据向导的答案添加 Elmah 或 ApplicationInsights。 I tried doing the following which didn't work:我尝试执行以下操作但不起作用:

<Reference Include="elmah, Version=1.2.2, Culture=neutral" Condition="$($ext_custommessage$) == 'LAN'" />

I tried adding it in a <ItemGroup> element but no dice.我尝试将它添加到<ItemGroup>元素中,但没有骰子。

What should I do in this case to add these packages to the .csproj file?在这种情况下我应该怎么做才能将这些包添加到.csproj文件中?

I think that the easiest way to do this is to have both packages installed.我认为最简单的方法是安装两个软件包。 Then only configure ELMAH when running on localhost.然后只在 localhost 上运行时配置 ELMAH。 I'm guessing you have web.config and web.release.config files.我猜你有web.configweb.release.config文件。 You then include ELMAH config in the web.config one but remove it using XDT in the web.release.config one.然后,您在 web.config 之一中包含 ELMAH 配置,但在web.config之一中使用 XDT 将其web.release.config It's basically described in reverse here: https://docs.elmah.io/use-multiple-logs-for-different-environments/#logging-to-elmahio-from-production-only (only logging from production).这里基本上是反向描述的: https://docs.elmah.io/use-multiple-logs-for-different-environments/#logging-to-elmahio-from-production-only (仅从生产记录)。 Also, the example is for elmah.io (cloud version of ELMAH) but you can use any logger implementation you'd like.此外,该示例适用于 elmah.io(ELMAH 的云版本),但您可以使用任何您喜欢的记录器实现。 I'm not sure how you'd configure AI but you can probably use a similar approach there.我不确定您将如何配置 AI,但您可能可以在那里使用类似的方法。

I achieved the goal by doing the following:我通过执行以下操作实现了目标:

  • Added two custom parameters to the custom wizard that's connected to the templates.向连接到模板的自定义向导添加了两个自定义参数。 One parameter for the package name/id and one for the package version. package 名称/id 的一个参数和 package 版本的一个参数。 The values of these parameters are set based on user's choice in the wizard.这些参数的值是根据用户在向导中的选择设置的。 These two parameters are passed using Dictionary<string, string> replacementsDictionary in the RunStarted method in the custom wizard.这两个参数是使用自定义向导的RunStarted方法中的Dictionary<string, string> replacementsDictionary传递的。

  • Added the following line to packages.config file that's in the project template folder:将以下行添加到项目模板文件夹中的packages.config文件中:

<package id="$package$" version="$version$" targetFramework="net48" />

$package$ is the parameter that has the package name. $package$是具有 package 名称的参数。 $version$ is the version of the package. $version$是 package 的版本。

I also added the following to the .vstemplate file of the project:我还在项目的.vstemplate文件中添加了以下内容:

<ProjectItem ReplaceParameters = "true" TargetFileName="packages.config" >packages.config</ProjectItem>

This is needed so that the parameters get replaced with the wanted package information.这是必需的,以便将参数替换为所需的 package 信息。

Once user runs update-package -reinstall in package console manager, the required packages will be installed as well as the other ones will be restored.一旦用户在 package 控制台管理器中运行update-package -reinstall ,将安装所需的软件包以及恢复其他软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM