How to set log levels via command line arguments in .NET Core 3 - .net-core-logging

I have a .NET Core 3.0 console application. I have a Microsoft.Extensions.Logging logger and I'm building Microsoft.Extensions.Configuration using a command line arguments.
My question is "How can I set the log levels via command line arguments?"

As pointed out in the Configuration in ASP.NET Core docs the arguments can be passed in one of the following ways:
No prefix CommandLineKey1=value1
Two dashes --CommandLineKey2=value2, --CommandLineKey2 value2
Slash (/) /CommandLineKey3=value3, /CommandLineKey3 value3
Example:
# Setting Warning level for all microsoft types
dotnet your.dll --Logging:LogLevel:Microsoft=Warning
# or setting Information level for your own types
dotnet run /Logging:LogLevel:YourNamespace:YourType Information

Related

Getting database errors after adding files in web.config transformation process

I need three config files under webconfig for three different environments i.e QA,Pre-PROD,PROD. I have added through configuration manager in VSTS and built successfully and pushed in Azure devops repo then tried to build it.Its throwing database error. Please suggest me how to add three webconfig files without getting threse errors. In build pipeline we are using Visual studio build and In release pipeline IIS web app deploy task and enable XML transformation
[![Please see the picture for scrrenshot of task and settings][1]][1]
[![Build pipeline][2]][2]
XXXX.Database.Deployment\dbo\Views\ProjectLocations.sql(51,1): Error SQL70015: Stored procedure 'XXXX' is not supported for the targeted platform.
XXXX.Database.Deployment\dbo\Tables\DataAccessRequests.sql(26,5): Error SQL70015: Keyword or statement option 'XXXX' is not supported for the targeted platform.
XXXX.Database.Deployment\dbo\Tables\PermissionRequests.sql(30,5): Error SQL70015: Keyword or statement option 'XXXX' is not supported for the targeted platform.
XXXX.Database.Deployment\dbo\Views\vDataAccessRequestDetails.sql(33,6): Error SQL71501: View: [dbo].[vDataAccessRequestDetails] has an unresolved reference to object [dbo].[DataAccessRequests].

asp.net core 2.1 HTTP Error 502.5

When I am trying to run my solution I am getting In the browser:
HTTP Error 502.5 - Process Failure
Debugging stops almost immediately after it starts and there is no error message inside visual studio.
In the output window this is the only message:
The program '[30700] dotnet.exe' has exited with code -2147450730 (0x80008096).
The project was working fine, I just installed a nuget package and this started happening. I tried deleting it and removing the package cache but it still happens.
Event viewer shows error with IIS Express AspNetCore Module.
Application 'MACHINE/WEBROOT/APPHOST/PROJECTNAME' with physical root 'E:\path\ProjectName\' failed to start process with commandline 'e:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\web tools\projectsystem\VSIISExeLauncher.exe -argFile "C:\Users\username\AppData\Local\Temp\tmp49E1.tmp"', ErrorCode = '0x80004005 : 0.
I am a bit lost as there is no error to go on.
Any ideas?
Well! This is due to appropriate .NET Core SDK missing problem. Your project's package versions are higher than the SDK version installed on your machine.
Download the latest version v2.1.401 (at the time of that answer) from here: Download .NET Core SDK and install it.
Now restart your computer and run the project again.
Hope your problem will be solved!
Although it was already answered, I'll post here my solution for the same problem (HTTP Error 502.5 when starting my webapp on Asp.NET CORE 2.1, error code 0x80004005) that has a different reason, as reference of another possibility.
Short Answer:
If the name of the application has a space (character) on it, the current version of Visual Studio (15.8.9) has a bug, that doesn't include quotes to make it a literal string argument on the moment of execution (through commandline) on the web.config file, generated when publishing your webapp.
Example:
web.config generated by Visual Studio (version 15.8.9 - Date: 2018-Nov-05):
[...]
<aspNetCore processPath="dotnet" arguments=".\My Web App.dll" stdoutLogEnabled=... />
[...]
web.config with correct quotation:
[...]
<aspNetCore processPath="dotnet" arguments='".\My Web App.dll"' stdoutLogEnabled=... />
[...]
You can see on the attribute arguments on the second example, that I included (manually) single quotes, making it pass the full string ".\My Web App.dll" as the argument on the moment of execution.
Detailed Answer:
Every time I update my Visual Studio for a newer SDK (and using the most recent version of Asp.net Core in my application) it give me the Http Error 502.5, IF i do not update the runtime libraries on my server too. So, obviously the first thing that I did was update the runtime libraries on the server (which always solved this problem to me), but this time it didn't worked.
So, starting to troubleshoot, I just tried to start my webapp from the command line (prompt), and it started perfectly.
So, I knew that there was something wrong with the way my webapp was being started. The starting configuration (on asp.net core) is in the web.config file.
[...]
<aspNetCore processPath="dotnet" arguments=".\My Web App.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
[...]
In Asp.net Core, the web application is started/executed from the commandline (prompt) on windows, by IIS (i'm strictly speaking of common scenario).
Till now, it always had a .exe file between the published files, that started the whole thing, and this was the file called by IIS Module through commandline.
Since Asp.net CORE 2.1 (and some minor update that i don't know exactly), there is no .exe file, and the PATH of Web App is passed by argument. So you have the command that is dotnet and a argument to this command that is specified as argument attribute, on the web.config file. (as shown in the example above)
Taking a look on my published web.config file, you can see the processPath and arguments attributes. In the previous test, I knew that dotnet was a recognized command, since I was able to start my webapp on windows command prompt. Then, looking carefully the arguments attribute, I saw that there was no (surrounding) quotation for the value, that contained space characters in it.
So in the startup of my webapp, instead the dotnet command receive the full path .\My Web App.dll, it was receiving 3 different arguments: .\My , Web , App.dll .
Since the value of the arguments attribute is passed through commandline, it has to have quotation when passed to the commandline, becoming a literal string.
So I mannualy added the necessary surrounding quotation on the arguments attribute value, in the web.config file, and my web app started to work perfectly!
To see the example of the bug, and how to correct it, just take a look on the "Short Answer" examples.
Other useful information (for Asp.net Core 2.0 and up, with Windows / IIS):
If you had a web application that was working, and is not anymore, giving the error 502.5:
It's probably a versioning problem of the runtime libraries. Your webapp is asking for newer asp.net core libraries, and your server doesn't have them yet.
Just update the runtime libraries on your server, and it should solve the problem. Download it from Microsoft (for the current version which is Asp.net Core 2.1, you can download from this link: https://www.microsoft.com/net/download/dotnet-core/2.1).
How to start your webapp manually, for better troubleshooting:
Open the windows command prompt, and try to execute the command dotnet. If it's not recognized, than you have to install (or repair) the asp.net core module and dependencies (google is your friend. Just search how to install asp.net core). Alternatively, you can check the Asp.net Core runtime libraries version with the command dotnet --version.
Once the dotnet command is recognized, you can start your webapp manually.
Navigate to the folder where is your webapp files (usually will be in inetpub, wwwroot, etc..). Then, locate the .dll file that is your application's assembly. Usually, it will have the name of your application (pretty easy, right!?). Then, execute it with the command dotnet ".\My Web App.dll".
Example:
If there is a error, you will see some useful information on the prompt window. If the webapp starts correctly, then it's some issue with the startup configuration, probably web.config file.
Another method to see more detailed information on asp.net core failure:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/troubleshoot?view=aspnetcore-2.1#application-event-log
Access the Application Event Log:
Open the Start menu, search for Event Viewer, and then select the Event Viewer app.
In Event Viewer, open the Windows Logs node.
Select Application to open the Application Event Log.
Search for errors associated with the failing app. Errors have a value of IIS AspNetCore Module or IIS Express AspNetCore Module in the
Source column.
Details of my issue:
When executing requests, the error presented was: HTTP Error 502.5 - Process Failure.
Looking on the Events Viewer (Windows Server), the information was: "Application ' ... My Web App' with physical root 'C:\ ... \ ... \' failed to start process with commandline 'dotnet .\My Web App.dll', ErrorCode = '0x80004005 : 1."
So my error code was: 0x80004005 , and the subcode was 1.
Hope it helps somebody :)

Launch IIS Express to run ASP.NET Core Apps

I wish to run my ASP.NET Core App by launching it from IIS Express using command line.
I stumbled across this article which says
So in fact Visual Studio silently adds the two environment variables
when launching IIS Express, so that ASP.NET Core related bits can be
injected.
LAUNCHER_ARGS: -debug -p “C:\Program Files\dotnet\dotnet.exe” -a “exec
\”C:\Users\lextm\documents\visual studio
2017\Projects\WebApplication2\WebApplication2\bin\Debug\netcoreapp1.0\WebApplication2.dll\””
-pidFile “C:\Users\lextm\AppData\Local\Temp\2\tmpFD6D.tmp” -wd “C:\Users\lextm\documents\visual studio
2017\Projects\WebApplication2\WebApplication2”
The tmp file in -pidFile “C:\Users\lextm\AppData\Local\Temp\2\tmpFD6D.tmp” can always change. How do I add LAUNCHER_ARGS as environment variable which will make it work even if the tmp file changes?
Let me know if there is any easier way to launch IIS Express to run ASP.NET Core Apps with command line or powershell scripts.
You are looking for [System.IO.Path]::GetTempFileName() method. It creates empty temp file on file system and returns its unique name.
I'm currently using the following PowerShell script to run my .NET Core 2.0 App:
$env:LAUNCHER_ARGS = "-p ""<path to dotnet.exe>"" -a ""exec \""<path to webapp main dll>\"""" -pidFile $([System.IO.Path]::GetTempFileName()) -wd ""<path to webapp root folder>"" -pr <project name>"
$env:LAUNCHER_PATH = "<path to VSIISExeLauncher.exe>"
& "<path to iisexpress.exe>" /config:"<path to applicationhost.config>" /site:"<webapp name>"
Placeholders (text within angle brackets) have to be filled with the corresponding values. You can find them out by running your project from Visual Studio and inspecting environment variables of iisexpress.exe process using Process Explorer as shown above in the link you provided.
In .NET Core 3 the solution to this problem has changed. Follow these steps.
1) The environment variables should now be:
LAUNCHER_ARGS=exec "C:\YourWebApiProject\bin\Debug\netcoreapp3.1\YourWebApiProject.dll"
LAUNCHER_PATH=C:\Program Files\dotnet\dotnet.exe
Change the first path to your dll path and ensure the .NET version in the path is correct. Note that there is no longer a need to create a temp file.
2) Ensure that both modules AspNetCoreModule and AspNetCoreModuleV2 are registered in the file .vs\{your solution name}\config\applicationhost.config as follows:
Under <system.webServer> <globalModules> add:
<add name="AspNetCoreModule" image="%IIS_BIN%\aspnetcore.dll" />
<add name="AspNetCoreModuleV2" image="%IIS_BIN%\Asp.Net Core Module\V2\aspnetcorev2.dll" />
Under <sectionGroup name="system.webServer"> add:
<section name="aspNetCore" overrideModeDefault="Allow" />
Under <location path="" overrideMode="Allow"> <system.webServer> <modules> add:
<add name="AspNetCoreModule" lockItem="true" />
<add name="AspNetCoreModuleV2" lockItem="true" />
It's also a good idea to make this change to the templates for this file which are located at %PROGRAMFILES%\IIS Express\config\templates\PersonalWebServer\applicationhost.config and %PROGRAMFILES(x86)%\IIS Express\config\templates\PersonalWebServer\applicationhost.config so that new VS solutions you create automatically get these changes to their configs. (Credit to this post)
3) Be mindful of whether you're using 32 or 64 bit IIS Express. (If you're on a 64 bit machine then 32 bit IIS Express = C:\Program Files (x86)\IIS Express, 64 bit = C:\Program Files\IIS Express) In my case, 32 bit had worked fine previously but after migrating to .NET Core 3 I had to use 64 bit or else the above modules wouldn't load.
I needed to run multiple .Net Core API endpoints at a time easily without popping open Visual Studio for each and every one of them. I ended up using answers here to build the following:
iisaspnet.bat:
#echo off
:: Args are like:
:: MobileApi
:: C:\Users\chris\Dropbox\Code\2017\VbaMeasureHcp\src\HCPMobileApi
:: MobileApi\bin\Debug\netcoreapp2.2\MobileApi.dll
:: .vs\MobileApp\config\applicationhost.config
setlocal
set PATH=%PATH%;C:\Program Files\IIS Express
set LAUNCHER_ARGS=exec %2\%3
set LAUNCHER_PATH=C:\Program Files\dotnet\dotnet.exe
iisexpress /site:%1 /config:"%2\%4"
:: Comment out line below to check for errors
exit
The first arg is the name of the Project - the name in Visual Studio (in some scenarios people go rogue and name their Project file one thing and the Project itself another thing - you want the Project name, not the file name, in this scenario).
The second arg is the root folder for third and fourth args.
The third arg is where to find the compiled Project DLL
The fourth arg is where to find the applicationhost.config that explains how to launch the site. As you'll see below, this is generally found in your .vs folder, but, where this exists can get a little crazy depending on how creative people get with organizing their Solution and Project folders. Generally the .vs folder is going to sit in the same folder as the .sln file, so it may be far from the Project folder/files.
This will be less helpful, but here's the batch file that kicks off the IIS Express windows, so you can see iisaspnet.bat in use:
start iisaspnet.bat MobileApi C:\Users\chris\Dropbox\Code\2017\VbaMeasureHcp\src\HCPMobileApi MobileApi\bin\Debug\netcoreapp2.2\MobileApi.dll .vs\MobileApp\config\applicationhost.config
start iisaspnet.bat HCP.MasterDataApi C:\Users\chris\Dropbox\Code\2017\VbaMeasureHcp\src MasterData\Api\HCP.MasterDataApi\bin\Debug\netcoreapp2.2\HCP.MasterDataApi.dll Solutions\.vs\MasterData\config\applicationhost.config
start iisaspnet.bat HCP.SecurityApi C:\Users\chris\Dropbox\Code\2017\VbaMeasureHcp\src Security\Api\HCP.SecurityApi\bin\Debug\netcoreapp2.2\HCP.SecurityApi.dll Solutions\.vs\Security\config\applicationhost.config
start iisaspnet.bat HCP.BillingApi C:\Users\chris\Dropbox\Code\2017\VbaMeasureHcp\src Billing\Api\HCP.BillingApi\bin\Debug\netcoreapp2.2\HCP.BillingApi.dll Solutions\.vs\Billing\config\applicationhost.config
start iisaspnet.bat HCP.ClientApi C:\Users\chris\Dropbox\Code\2017\VbaMeasureHcp\src Client\Api\HCP.ClientApi\bin\Debug\netcoreapp2.2\HCP.ClientApi.dll Solutions\.vs\Client\config\applicationhost.config
start iisaspnet.bat HCP.EmployeeApi C:\Users\chris\Dropbox\Code\2017\VbaMeasureHcp\src Employee\Api\HCP.EmployeeApi\bin\Debug\netcoreapp2.2\HCP.EmployeeApi.dll Solutions\.vs\Employee\config\applicationhost.config
The way the parameters work could likely be far simpler if the way the Projects, Solutions, etc were stored and named was more consistent, but, this is an existing set of Solutions I had no control over with scattered naming etc, and, the chaos above may be more helpful anyway for understanding how to call these commands.
The result is that running one command kicks off 6 IIS Express command windows for me, requests get logged to each of their windows, and I just type Q in each window to kill them.

ASP.NET 5 site fails to publish to azure appservice because of a long path

I'm trying to publish a relatively new ASP.NET site to azure appservice from Visual Studio.
I'm getting the following error message during the preview:
The "Dnu" task failed unexpectedly.
System.Exception:
Microsoft .NET Development Utility Clr-x86-1.0.0-rc1-16231
Copying to output path C:\Users*****\AppData\Local\Temp\PublishTemp*****************.********.WebApi114
Error: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
at Microsoft.DNX.Tasks.Dnu.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()
Path is long indeed. How do I fix it?
Error is from windows file API, not control by Azure App Service. Sadly I think you will have to find a way to shorter your file path in order to fix this issue.
I was running into the same issue trying to build an Angular2 app locally. So I tend to agree with Xiaomin that it may be a local issue. What worked for me was to run the dnu publish command from a command prompt with the output flag set with a shorter folder destination provided. For example, navigate to the location of the project you want to publish in a command prompt and type:
"dnu publish --runtime active -o c:\Sample"
The above command will post your output to c:\Sample.
You can interrogate the options as follows:
"dnu -help"
"dnu publish -help"

How to add arguments for run command in Mule Anypoint Studio

I'm trying to use Mule Credentials Vault security feature from Anypoint Studio. As in Mule's example I need to have:
.properties file with encrypted data
Global element - Similar to Mule's example
In mule-app.properties - similar to Mule's example:
When running it from command line I put the password as argument:
The error I get is:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'key' threw exception; nested exception is java.lang.RuntimeException: Property code could not be found
When I enter the password directly to the global element the app is deployed and running.
How can I insert the password on runtime (similar to how I enter it via command line)?
Thanks,
Keren
The -M is a way of passing arguments to the JVM if you are using the standalone Mule only. In studio you just need to pass -D.
If you want to set the code property through JVM argument, insert the -M-Dcode=24681357 in the VM arguments (and in the program arguments).
If you want to set the code property in the mule-app.properties, the line should be code=24681357 instead of -M-Dcode=24681357.
Thoose are two ways of setting properties in mule (can use wrapper.conf too). You should choose the one that fits your needs (and not use both simultaneously)