Log using NLog with Azure Devops - asp.net-core

I am using asp.net core with NLog, here is the NLog target config
<target name="file" xsi:type="File"
layout="${longdate} ${logger} ${message}${exception:format=ToString}"
fileName="${basedir}/logs/logfile-${shortdate}.txt"
keepFileOpen="true"
encoding="utf-8" />
<target xsi:type="Null" name="blackhole" />
When I run from Visual Studio (F5), asp.net create logs folder and generate file like above configuration.
But when I deploy to Azure Webapp by Azure DevOps, it couldn't generate log file.
Is there anyway to fix this issue?

What happens if you change the NLog.config to be included in publish. Update csproj-file:
<None Update="nlog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
The special one is CopyToPublishDirectory

Related

Config Transform in Azure DevOps

I would like to use a visual studio proj file to transform xml files. I am following this article. http://sedodream.com/2010/04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx . This works for me locally, however when deploying the application on Azure DevOps it fails. It cannot find Microsoft.Web.Publishing.Tasks.dll. How do I set up a build task that will only transform the config files.
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TransformXml"
AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v15.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
<Target Name="Build">
<TransformXml Source="Web.config"
Transform="Web.Release.config"
Destination="Web.Production.config" />
</Target>
</Project>
Turned out to be an easy fix. It was an old build so that the hosted agent. Just had to change it to the Hosted VS2017 agent.
AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
I think that's why the issue happens. For a vs2017 agent, you need to change the v10.0 to v15.0, so that the msbuild tool can find the assembly.
Also, as for vs2017, make sure you set the correct msbuild tool path in VSTS like this issue.

How exclude certain appsettings files from publishing

I have asp.net core 2 application. Based on documentation i have created .pubxml file for my development environment. I am using web deployment (remote agent) to deploy the web package to target web server. On our build server where, jenkins is installed, i run the following command to build & deploy
D:\Jenkins\myproject\workspace\myapplication\Src\Api>dotnet publish Api.csproj /p:PublishProfile=development.pubxml
It builds and deploys the application successfully to target web server.
However my application has multiple appsettings files specific to each environment. For example appsettings.json, appsettings.development.json, appsettings.staging.json and appsettings.production.json The deployment process deploys all the appsettings files to web server. (in this case to development web server)
How do i only include environment specific appsettings file? (in this case i want to deploy appsettings.json, appsettings.development.json but not other 2 files)
You can add <ItemGroup> definitions to your publish profiles (.pubxml files) containing update statements for the files.
For example if the publish profile is named Production.pubxml/ Staging.pubxml according to the environment, you can add this item group to the root <Project> xml node:
<ItemGroup>
<Content Update="appsettings.*.json" CopyToPublishDirectory="Never" />
<Content Update="appsettings.$(MSBuildThisFileName).json" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
I could handle this with the following *.pubxml:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
.
.
.
</PropertyGroup>
<ItemGroup>
<Content Update="appsettings.Development.json">
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.TestBed.json">
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

how to change default siteName and its physical path using msdeploy

Here i'm trying to automate publish the package for a web application to publish IIS on a remote server.
Following things are done to make the package.
Publish.proj.xml file:
<Project ToolsVersion="4.0" DefaultTargets="Publish" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectSolutionFile>Test.sln</ProjectSolutionFile>
<Configuration>Release</Configuration>
</PropertyGroup>
<ItemGroup>
<ProjectToBuild Include="$(ProjectSolutionFile)">
<Properties>DeployOnBuild=true;Configuration=$(Configuration)</Properties>
</ProjectToBuild>
</ItemGroup>
<Target Name="Publish">
<MSBuild Projects="#(ProjectToBuild)"/>
</Target>
</Project>
If i pass this Publish.proj.xml file to msbuild. it'll build the solution and will create the package with below included things,
1. PackageTmp (folder)
2. Test.deploy.cmd
3. Test.deploy.SetParameters.xml
4. Test.SourceManifest.xml
5. Test.zip
Test.SetParameters.xml
<?xml version="1.0" encoding="utf-8"?>
<parameters>
<setParameter name="IIS Web Application Name" value="Default Web Site/Test_deploy" />
</parameters>
Test.SourceManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<sitemanifest>
<IisApp path="C:\Release\Package\PackageTmp" managedRuntimeVersion="v4.0" />
<setAcl path="C:\Release\Package\PackageTmp" setAclResourceType="Directory" />
<setAcl path="C:\Release\Package\PackageTmp" setAclUser="anonymousAuthenticationUser" setAclResourceType="Directory" />
</sitemanifest>
Now, i'm trying to install this package in a remote server to publish the package using below command,
SalesCatalogue.deploy.cmd /y /M:https://RemoteServer:8172/MSDeploy.axd /u:"Test" /p:Test123 -allowUntrusted /A:basic
It has created new virtual directory in IIS under Default Web Site->Test_deploy and package files are deployed in C:\inetpub\wwwroot\Test_deploy folder.
My requirement is, Virtual directory name, applicatio Pool, destination physical deployment path are should be configurable. So that I can provide name whatever for virtual directory, can select desire application pool and can deploy in different path if required.
How can i achieve this? Any one can suggest me.
Thanks in advance.

Different msbuild behavior?

While creating deployment project for my solution strange behavior have been noticed.
I'm using webdeploy for deployment of my web apps.
And when putting this
<Exec Command="$(MsBuildBinPath)\msbuild.exe $(SourceControlPath)\...\myproject.csproj /p:configuration=Release /p:DeployOnBuild=True /p:DeployTarget=MSDeployPublish /p:MSDeployServiceUrl=https://x.x.x.x:8172/msdeploy.axd /p:MSDeployPublishMethod=WMSvc /p:DeployIisAppPath="Default Web Site" /p:username=username /p:password=password /p:AllowUntrustedCertificate=True /P:CreatePackageOnPublish=True">
</Exec>
in my deploy project file, it builds and deploys my application without issues.
But using MSBuild task with exact same set of properties
<MSBuild Properties="Configuration=$(Configuration);DeployOnBuild=True;
DeployTarget=MSDeployPublish;MSDeployServiceUrl=https://x.x.x.x:8172/msdeploy.axd;
MSDeployPublishMethod=WMSvc;DeployIisAppPath="Default Web Site";
UserName=username;Password=password;
AllowUntrustedCertificate=True;CreatePackageOnPublish=True"
Projects="$(SourceControlPath)\...\myproject.csproj"
></MSBuild>
gives me an error: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.5\Web\Microsoft.Web.Publishing.targets(4196, 5): error ERROR_COULD_NOT_CONNECT_TO_REMOTESVC: Web deployment task failed. (Could not connect to the remote computer ("x.x.x.x") using the specified process ("Web Management Service") because the server did not respond. Make sure that the process ("Web Management Service") is started on the remote computer...
So, I was thinking that basically both approaches do the same. what's wrong with 2-nd approach?
The problem may be the escaped quote you have the Properties attribute. Have you tried not including the " around Default Web Site?
One way I have used to make the file more readable is pass the arguments via an item group.
<ItemGroup>
<DeployArgs Include="Configuration=$(Configuration)" />
<DeployArgs Include="DeployOnBuild=True" />
<DeployArgs Include="DeployTarget=MSDeployPublish" />
<DeployArgs Include="MSDeployServiceUrl=https://x.x.x.x:8172/msdeploy.axd" />
<DeployArgs Include="MSDeployPublishMethod=WMSvc" />
<DeployArgs Include="DeployIisAppPath=Default Web Site" />
<DeployArgs Include="UserName=username" />
<DeployArgs Include="Password=password" />
<DeployArgs Include="AllowUntrustedCertificate=True" />
<DeployArgs Include="CreatePackageOnPublish=True" />
</ItemGroup>
<MSBuild Properties="#(DeployArgs)"
Projects="$(SourceControlPath)\...\myproject.csproj"
/>

Msbuild just produces a package but does not deploy it when using a publish profile. Why?

MSBuild really seems to like me.
Recently I am trying out the different possibilities to build and deploy from command line.
However I am experiencing some seemingly strange behaviour when I pass a publish profile to MSBuild.
Here is an example of what I just did:
I deploy to a local IIS for the moment with a command such as this:
msbuild D:\PathToFile\DeployDBVariation01\DeployDBVariation01\DeployDBVariation01.csproj
/p:Configuration=Release;
Platform=AnyCpu;
DeployOnBuild=true;
DeployTarget=MSDeployPublish;
MSDeployServiceURL="localhost";
DeployIisAppPath="DeployApp/DeployThis";
MSDeployPublishMethod=InProc;
Username=thisIsNotActuallyMyUsername;
password=guesswhat;
AllowUntrustedCertificate=true
And this works! After that it is successfully deployed and I can call it in a browser.
However, since Visual Studio gives us the comfort of using publishing profiles I wanted to try that in conjunction with MSBuild over command line and tried the following command:
msbuild D:\PathToFile\DeployDBVariation01\DeployDBVariation01\DeployDBVariation01.csproj
/p:DeployOnBuild=true;
AllowUntrustedCertificate=true;
PublishProfile=ReleaseLocal
ReleaseLocal is a profile I created in Visual Studio and it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>MSDeploy</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<ExcludeApp_Data>False</ExcludeApp_Data>
<MSDeployServiceURL>localhost</MSDeployServiceURL>
<DeployIisAppPath>DeployApp/DeployThis</DeployIisAppPath>
<RemoteSitePhysicalPath />
<SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
<MSDeployPublishMethod>InProc</MSDeployPublishMethod>
<EnableMSDeployBackup>False</EnableMSDeployBackup>
<UserName />
<_SavePWD>False</_SavePWD>
<PublishDatabaseSettings>
<Objects xmlns="">
<ObjectGroup Name="DefaultConnection" Order="1" Enabled="False">
<Destination Path="Data Source=.\SQLEXPRESS;Initial Catalog=HorstDataProductive;User ID=sa;Password=GuessWhat" />
<Object Type="DbDacFx">
<PreSource Path="Data Source=.\SQLEXPRESS;Initial Catalog=HorstData;User ID=sa;Password=GuessWhat" includeData="False" />
<Source Path="$(IntermediateOutputPath)AutoScripts\DefaultConnection_IncrementalSchemaOnly.dacpac" dacpacAction="Deploy" />
</Object>
<UpdateFrom Type="Web.Config">
<Source MatchValue="Data Source=.\SQLEXPRESS;Initial Catalog=HorstData;User ID=sa;Password=GuessWhat" MatchAttributes="$(UpdateFromConnectionStringAttributes)" />
</UpdateFrom>
</ObjectGroup>
</Objects>
</PublishDatabaseSettings>
</PropertyGroup>
<ItemGroup>
<MSDeployParameterValue Include="$(DeployParameterPrefix)DefaultConnection-Web.config Connection String">
<ParameterValue>Data Source=.\SQLEXPRESS;Initial Catalog=HorstDataProductive;User ID=sa;Password=GuessWhat</ParameterValue>
</MSDeployParameterValue>
</ItemGroup>
</Project>
As you can see I have some additional connection string replacement there that I want to test.
So I execute that last MSBuild-command that I have shown you and it executes without any errors. Instead it says that the web deployment task was successful and that a package has been created in a certain path. Now that is actually the right package. When I import that manually in my IIS it is the result I expect, also the connection string replacement has been done.
But I do not understand why it actually is just creating the package but not deploying it in one run, like in my first command.
Can someone explain?
(Or even better, what do I have to do to make it also deploy that package immediately)
You need to specify the VS version.
http://www.asp.net/mvc/tutorials/deployment/visual-studio-web-deployment/command-line-deployment
msbuild /P:DeployOnBuild=True /P:VisualStudioVersion=11.0 /P:PublishProfile=Dev.pubxml
Don't forget to allow untrusted certs if you're using a 'fake' internal certificate
Looks like you're missing the deploy target.. it's got all the necessary info, but doesn't know what you want it to do. Try this;
msbuild D:\PathToFile\DeployDBVariation01\DeployDBVariation01\DeployDBVariation01.csproj
/p:DeployOnBuild=true;DeployTarget=MSDeployPublish;AllowUntrustedCertificate=true;PublishProfile=ReleaseLocal