VS Code: can't compile c# - c#

When I click 'Start Debugging' in visual code I receive this error. No google results have worked so how do I fix it? I'm trying to compile C# code.
The preLaunchTask 'build' terminated with exit code 1.
I have added some code below of my launch.json file and the other file for my VS code as I wasn't sure if it would help being able to see them.
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false,
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceRoot}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [ ],
"isBuildCommand": true,
"showOutput": "silent",
"problemMatcher": "$msCompile"
}
]
}

There's a preLaunchTask configured in your launch.json file. It is the task that gets run before the script is executed. That task is 'build', from your tasks.json file. It is failing, as indicated by its return of a non-zero error code. You need to open up your tasks.json configuration file, see what the 'build' task entails, and then figure out why it is failing.

Related

C# Serilog - ApplicationInsights - ASP.NET Core 6 - Own Exception logging with ILogger

How can i set ApplicationInsights to send Ilogger _logger messages.
Now i see every log at Console and in files, but in azure i don't see my own Logs.
I tried a lot of things and maybe the information is here:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-monitoring
But i don't find it. :(
Thanks for helping.
In Program.cs:
var _logger = new LoggerConfiguration().CreateBootstrapLogger();
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
.ReadFrom
.Configuration(builder.Configuration)
.Enrich
.FromLogContext());
Bootstrap.ConfigureServices(builder.Services, builder.Configuration, builder.Environment);
var app = builder.Build();
Bootstrap.ConfigureWebApp(app);
app.Run();
In appsettings.json:
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.File",
"Serilog.Sinks.ApplicationInsights"
],
"MinimumLevel": {
"Default": "Debug"
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"Path": "../Logs/MyLog-.json",
"RollingInterval": "Day",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
},
{
"Name": "File",
"Args": {
"Path": "../Logs/MyLog-.log",
"RollingInterval": "Day",
//"OutputTemplate": "[({Component}|{MachineName}|{ThreadId}) {Timestamp:G} [{Level:u3}]{Message:lj}{NewLine}{Exception} ]"
}
},
{
"Name": "ApplicationInsights",
"Args": {
"ConnectionString": "[MyCONNECTIONSTRING]",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
}
]
},

How can I force VS Code to debug as sudo?

This is the content of my tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"/Crm/AdminApi/Api.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": ""
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"/Crm/AdminApi/Api.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": ""
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"/Crm/AdminApi/Api.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": ""
}
]
}
The problem is that when I run it using F5 it fails because it runs as the vscode user I guess.
Also when I run dotnet build from inside the VS Code terminal, it does not work.
However, when I run sudo dotnet build in the VS Code terminal, or when I run dotnet build from a root bash terminal, it works.
How can I force VS Code to run my code as the root user? I tried "command": "sudo dotnet" but it did not work.

serilog not writing logs into local app data folder

I am trying to write log into "LocalApplicationData" using Serilog and using below configuration, but it's not working. If I try to write in some hard code path like "path": "C:/Temp/Logs/log.txt" then this is working. How to write into local app data folder?
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "${specialfolder:folder=LocalApplicationData}/Logs/log.txt",
"fileSizeLimitBytes": "5242880",
"rollingInterval": "Day",
"retainedFileCountLimit": "15",
This format is used in NLog. The equivalent is to use %LOCALAPPDATA% instead:
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "%LOCALAPPDATA%/Logs/log.txt",
"fileSizeLimitBytes": "5242880",
"rollingInterval": "Day",
"retainedFileCountLimit": "15",
Please note that this will only work in appSettings.json. To do the equivalent in C# you can use Environment.GetEnvironmentVariable("LocalAppData")

How to config `Serilog` to write to the application directory with the cofig file?

I'm using Serilog on a .net core.
I want to config the log path to the application directory.
I see there's an extension https://github.com/serilog/serilog-settings-configuration that enable Serilog to read from Configuration.
In the example , the path is configured as "%TEMP%\\Logs\\serilog-configuration-sample.txt".
How can I set it to the working directory?
I've searched on so, and know that it can be done by code, but it seems there's no one asking how to do this by the config file, i.e. appsettings.json.
Current configuration:
{
"Serilog": {
"Using": [
"Serilog.Sinks.File"
],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": { "path": "Logs\\serilog-configuration-sample.txt" }
}
],
"Enrich": [ "FromLogContext", "WithMachineName" ],
"Destructure": [
],
"Properties": {
}
},
"AllowedHosts": "*"
}
I want the log path to be set to the working directory.
But currently it's in "C:\Program Files\IIS Express".
Configuring path like Logs/log.txt will write log files under logs folder in working directory
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/log.txt"
}
}
Also you can check this answer for another option
You can add a "RollingFile" wich can write to a local path file. In this example I'm writing in a File inside of the root of my project as it shows bellow.
{
"Name": "RollingFile",
"Args": {
"pathFormat": ".\\Logs\\logs.txt",
"fileSizeLimitBytes": 1048576
}
},
Also the full json on appsettings.json end up like this (in case you need a full example)
...
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"System": "Debug",
"Microsoft": "Debug"
}
},
"WriteTo": [
{
"Name": "ApplicationInsightsEvents",
"Args": {
"instrumentationKey": "xxxxxxxxxx"
}
},
{
"Name": "RollingFile",
"Args": {
"pathFormat": ".\\Logs\\logs.txt",
"fileSizeLimitBytes": 1048576
}
},
{ "Name": "Console" },
{
"Name": "EventLog",
"Args": {
"source": "API NAME",
"logName": "CustomLog",
"restrictedToMinimumLevel": "Warning"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "API NAME"
}
}
...

Why is host.run() just opening localhost

In the kestrel part of my BuildWebHost, I can verify that the url is set to https://localhost/5000. However, when host.run() is called, only https://localhost is launched for some reason.
`
.UseKestrel(options =>
{
X509Certificate2 certificate = null;
if (!string.IsNullOrEmpty(certFileName) && !string.IsNullOrEmpty(certPassword))
{
certificate = new X509Certificate2(certFileName, certPassword);
url = "https://localhost:5000";
}
options.Listen(IPAddress.Loopback, 5000, listenOptions =>
{
if (certificate != null)
{
listenOptions.UseHttps(certificate);
}
});
})
`
Additional Information:
It's launched when I run Visual Studio Code. My configurations files all say localhost:5000 This used to work. I changed my program property in launch.json to point at net2.0 path and that's when it stopped.
launch.json contents:
`
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
// "program": "${workspaceFolder}/bin/Debug/netcoreapp1.1/TennisFolderAPI.dll",
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/TennisFolderAPI.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
},
"requireExactSource": false,
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
`
For Visual Studio in launchSettings.json use "launchUrl": "http://localhost:5000".
Launch.json is for Visual Studio Code.
https://www.google.com/amp/s/andrewlock.net/how-to-set-the-hosting-environment-in-asp-net-core/amp/

Categories