Base Directory path of different environments in nlog - asp.net-core

I have .net core web api project with Production,Stage and Development environments. i used {baseDir} in filename of nlog target but when i run project it store log file in bin\netcoreapp3.1\netcoreapp3.1 folder.
I have a folder Logs and i want to store logs in this folder.
same in all environments i want that.
Anyone know how to do that please help me !!
for more detail :
nlog.config
<variable name="DefaultLayout" value="${longdate} ${processid} ${uppercase:${level}} ${logger:shortName=true} ${environment-user} ${local-ip} ${message} "/>
<targets async="true">
<target xsi:type="File" name="file" layout="${DefaultLayout}" fileName="${baseDir}\log-${shortdate}.log" />
</targets>
Thank you !!

The {baseDir} is the directory from which the code is executing, so what you're getting is correct.
If you want to use the content root path of your ASP.NET Core app you need to use ${aspnet-appbasepath} - ref.
You can specify it as fileName="${aspnet-appbasepath}\Logs\log-${shortdate}.log".
From the docs:
Introduced with NLog.Web.AspNetCore v4.5.0 and NLog.Web v4.5.2
Make sure you're targeting the respective version, otherwise you wouldn't be able to use ${aspnet-appbasepath}

Related

Is it possible to deploy an uncompiled ASP.NET Razor Pages website?

I am wondering if it is possible to copy the source code of an ASP.NET Razor Pages website to a Windows web server and for it to run without it needing to be published/compiled?
Similar to what you can do with ASP.NET Web Forms websites where you upload the source code (*.aspx and *.aspx.cs files) and they compile at runtime?
I saw something about Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation but I wasn't sure if this is what I was after and how to use it.
Is this possible and any guidance or links on how to do it?
PS. I'm sure this is not good practice, but would still like an answer... Thanks.
Is this possible and any guidance or links on how to do it?
PS. I'm sure this is not good practice, but would still like an answer
There's a way to allow deploy "uncompiled" *.cshtml. Assuming you're using ASP.NET Core 3.1:
First of all, add a package reference to Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation:
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.*" />
Change your services to allow Runtime Compilation:
services.AddRazorPages().AddRazorRuntimeCompilation();
Configure a custom Task to copy the source code to publish dir in your *.csproj file:
<ItemGroup>
<ViewFiles Include="$(ProjectDir)\Pages\**\*.cshtml" />
</ItemGroup>
<Target Name="CopyViewFilesAfterPublish" AfterTargets="Publish">
<Copy SourceFiles="#(ViewFiles)"
DestinationFolder="$(PublishDir)\Pages\%(RecursiveDir)"
/>
</Target>
Demo
I publish a RazorPage WebApp and host it on IIS. And then we can change the Pages/**/*.cshtml views dynamically:

How to specify a custom parameters.xml when building a Web Deploy package for ASP.NET Core?

Overview
I am building a deployable web package that can be imported into IIS that automatically prompts for settings needed by my ASP.NET Core application. I already created a package that will deploy just fine, except after deploying, I need to manually find/edit my appsettings.json file.
I know this package can include a parameters.xml file that will automatically prompt and fill in my appsettings.json when importing an app into IIS. I have already made a parameters.xml file, and manually added it to my package after building it; it worked as expected. I'd just like to have msbuild automatically add the parameters.xml file to the package for me.
A separate project of mine (ASP.NET MVC 4) already does this. For that, I simply needed to put my parameters.xml in the same folder as my .csproj. I tried doing the same here, but had no luck.
Repro Steps
I created an ASP.NET Core Web Application
Using .NET Framework on ASP.NET Core 1.1
I then went to publish my website
Selected 'Folder' (just to get a template)
I then edited the profile and changed the WebPublishMethod to Package and added the three lines below it.
<DesktopBuildPackageLocation>bin\$(Configuration)\$(MSBuildProjectName).zip</DesktopBuildPackageLocation>
<PackageAsSingleFile>true</PackageAsSingleFile>
<DeployIisAppPath>External</DeployIisAppPath>
I then published one more time. Now I get a WebDeploy package that I can deploy to IIS.
Great! but...
I'd like to customize the parameters.xml.
For previous projects, I was able to add a parameters.xml file to my project root, and VS/msbuild would automatically add it to my published package. This currently works for a different project using ASP.NET MVC 4.
So, I tried the same thing for this project. First I added a settings.json with a very simple setting:
{
"SettingName": ""
}
Then I added a parameters.xml file that I know works to my project root. (If I manually replace the parameters.xml file in Sample.zip package, it correctly prompts and replaces my setting when deploying)
<parameters>
<parameter name="IIS Web Application Name" value="External" tags="IisApp">
<parameterEntry kind="ProviderPath" scope="IisApp" match="^c:\\users\\joshs\\documents\\visual\ studio\ 2017\\Projects\\Sample\\Sample\\obj\\Release\\net461\\win7-x86\\PubTmp\\Out\\$" />
</parameter>
<parameter name="Setting Name" description="Enter a custom app setting" defaultValue="Default Setting Value">
<parameterEntry kind="TextFile" scope="obj\\Debug\\net461\\win7-x86\\PubTmp\\Out\\appsettings\.json$" match="(?<=\"SettingName\"\s*:\s*\")[^\"]*" />
</parameter>
</parameters>
Again, I right click and Publish once more. This time with the parameters.xml file.
I expect the Sample.zip to contain the parameters.xml that I added to my project root, but it does not. It is the exact same as from my original publish.
Question
During the build process when creating a web deploy package, how do you include custom settings in the parameters.xml?
I have already tried this...
I already looked at https://stackoverflow.com/a/46338042/2494785, but with no luck, though my command differed slightly from the original poster.
PS C:\Users\joshs\Documents\Visual Studio 2017\Projects\Sample> & 'C:\Program Files (x86)\Microsoft Visual Studio\2017\E
nterprise\MSBuild\15.0\Bin\MSBuild.exe' .\Sample.sln /t:Sample /p:DeployOnBuild=true /p:PublishProfile=FolderProfile /p:
ProjectParametersXMLFile="C:\Temp\parameters.xml"
I was able to solve this from peteawood's comment from an issue posted on GitHub.
https://github.com/aspnet/websdk/issues/201#issuecomment-349990389
In ASP.NET Core 2.0+ you can add the following to your .csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
.
.
<Target Name="AddMoreParameters" AfterTargets="_CreateParameterFiles">
<Copy SourceFiles="Parameters.xml" DestinationFiles="$(_MSDeployParametersFilePath)" />
</Target>
</Project>
SourceFiles should point to the location of your parameters.xml file from the perspective of the .csproj file. My parameters.xml is found in the same directory as my project file.
I believe I can just pass parameters via cmd-line as properties for msbuild.
It's not fully what you asked for I understand.
For example, in the following command I'm passing DeployIisAppPath property:
dotnet publish /p:WebPublishMethod=Package /p:DeployIisAppPath=mysite/myapp /p:PublishProfile=rnddev03-core-dev
and in the output folder we'll get xxx.SetParameters.xml file with:
<parameters>
<setParameter name="IIS Web Application Name" value="mysite/myapp" />
</parameters>

MSBuild: Build Custom Project Target When Solution Specified

I need to build at the solution level using MSBuild since I have multiple configurations. I have a custom target in my web project that publishes the output. I can specify the web project as the target and it builds fine. But I need to publish the web application. Is there a way to build a project's custom target when building at the solution level?
I found this link, but I don't understand the solution. If someone could break that down a little, it would help a lot.
This is what my current PowerShell command-line looks like:
& "$env:windir\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe" `
"..\..\Solution.sln" `
"/t:WebApplication:PublishToFileSystem" `
"/p:Configuration=$configuration;PublishDestination=$publishPath"
The :PublishToFileSystem causes it to fail, with this message:
error MSB4057: The target "PublishToFileSystem" does not exist in the project. [C:\Source\Solution.sln]
This is what my custom target in my .csproj file looks like:
<Target Name="PublishToFileSystem" DependsOnTargets="PipelinePreDeployCopyAllFilesToOneFolder">
<Error Condition="'$(PublishDestination)'==''" Text="The PublishDestination property must be set to the intended publishing destination." />
<MakeDir Condition="!Exists($(PublishDestination))" Directories="$(PublishDestination)" />
<ItemGroup>
<PublishFiles Include="$(_PackageTempDir)\**\*.*" />
</ItemGroup>
<Copy SourceFiles="#(PublishFiles)" DestinationFiles="#(PublishFiles->'$(PublishDestination)\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="True" />
</Target>
So, after some research, I found that creating a custom target is not the best way to publish a web application. Assuming you have just one web project in your solution, MS extended MSBuild to use publish profiles (the same ones used when you publish in Visual Studio). I was able to create a separate publish profile for each configuration (environment) and call the following command line:
& "$env:windir\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe" `
"..\..\Solution.sln" `
"/p:DeployOnBuild=true" `
"/p:PublishProfile=$configuration"
I just had to make sure my publish profiles had the same name as my $configuration.

how to publish my WCF Service Library using MSBuild Command Line in VS 2012?

I want to Publish my WCF Service Library using MSBuild Command Line with VS2012, i don't want to do right click->Publish Website , instead i want to publish it using Command Prompt(MSBuild).
What are the Pre-requisites required for MSBuild?
I don't have windows azure,it is necessary to install windows azure?
I am new to MSBuild, and I would like step by step instructions on how to accomplish this?
I want the .svc file, all the dll's inside the bin folder and web config file to be present inside the published folder.
I found the answer for my question with the reference as below link,
"http://www.asp.net/mvc/tutorials/deployment/visual-studio-web-deployment/command-line-deployment"
using the command line "msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true /p:PublishProfile=Test"
Your solution is fine and will work to deploy directly out to your host (don't forget to include your build Configuration so it publishes the correct config transform).
Another option is to "publish" your site to a local folder and then upload it to your host separately. This also gives you a chance to archive the site in a zip, do post checks and troubleshoot.
You can do this like so:
<MSBuild Projects="WebProject.csproj"
Targets="Rebuild;_WPPCopyWebApplication"
Properties="WebProjectOutputDir=WebProject\;UseWPP_CopyWebApplication=True;PipelineDependsOnBuild=False;" />
If you get stuck there are more details on publishing WCF, ASP.NET and MVC projects.
You can try with this.
The svc file was manually generated for first time use and then added to the project.
Content Include="*.svc" ---- Change this with the name of the file generated by VS Publish Option.
<ItemGroup>
<Content Include="*.svc">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<WebConfig Include="App.config" />
</ItemGroup>
<Target Name="AfterBuild">
<Copy SourceFiles="#(WebConfig)" DestinationFiles="$(OutDir)web.config" />
</Target>

How to handle Class Library/Solution/WebProject in MSBuild 2.0 and CrusiseControl.Net

I am working on CI tool CruiseControl.Net and MSBuild. I have many .csproj,.sln files and web projects (more than 30). We have 30 developers and they work on multiple projects in any given time.
As of now, the developer do not release .sln and .csproj files for build. Now my question is how to handle build file as :
1. Since developer does not release .csproj and how would i get newly added files required for compilation? And how would i get newly added reference both .Net Framework and Third-party dlls?
2. Some cases the developer opens WebProject independently and make changes and release. In this case, how would i compile it?
3. How would i manage order of project dependencies?
I am using .Net 2.0/.
Can anyone suggest and guide me here.
Thanks,
chandan
There are many ways to automate builds. One way I have used that could work for you too, is to use NAnt instead of MSBuild. Nant has a csc task that you can use. (CSC.exe being the csharp compiler itself)
<csc target="library" output="yourbuildpath\yourproject.dll" debug="${debug}">
<sources>
<include name="**\*.cs" />
<exclude name="**\AssemblyInfo.cs" />
</sources>
<references>
<include name="lib\*.dll" />
</references>
</csc>
This way you build anything that has a .cs extension instead of using the csproj or sln file. All developers are told to place referenced third party dll's in a known lib folder. I have used this only with Class Library and Web Application projects, I'm not sure it's as simple with Website projects.