Why won't my windows service create and write to log file? - c#

I have a windows service (.net 6), I am using Serilog.Extensions.Logging.File to add logging file, when I run exe file it creates a file and works fine, but when I install it as windows service it is not working. In Event log all looks good, if there is any error for service I can see it in event viewer log. (locally and in the server I have the same condition)
I am running under LOCAL SERVICE if that helps. It has Full access.
I have created the logs directory under my application folder.
appsetting.json file content:
"Logging": {
"File": {
"PathFormat": "Logs/log-{Date}.txt",
"fileSizeLimitBytes": 10485760,
"retainedFileCountLimit": 10
}
I checked the similar questions like this: Similar question not helpful for me

There was an issue in server which it was showing current working directory set to application folder but in fact it was saving in the windows folder, after restarting windows, it is solved.

A windows service, when registered and started, is not running from the folder from which you originally registered the service.
Instead, an instance of the service is held and the execution context is under the OS folder where services are held once registered.
Think of it as the "hosting directory" that you know from web-driven services proobably.
You can look up the directory of a service from the cmd with the command below:
sc qc <service name>

Related

I am not able to create msi setup using installshield for self hosted console application

I have created self-hosted WCF service using Console application. I have created msi using Installshield but when I am installing setup it will stuck and after some time failed to setup.
I have added log in code and found that on the line of hosting WCF service it was going to stuck and throw an error.
Any Idea how can I resolve this issue?
Service Install: Can you install and run it manually? You must install with admin rights, if you specify a user account to run the service it must have the "log on as a service" privilege (check by running secpol.msc or see here), the file you install must actually be a proper service - obviously - I sometimes mix up my test executables with the service binary. Ever forget? Happens to me :-).
Logging: Get yourself a proper log file first of all:
msiexec.exe /i "Setup.msi" /L*V "C:\Setup.log"
Or in silent mode:
msiexec.exe /i "Setup.msi" /QN /L*V "C:\Setup.log"
Some more information on Logging:
Installsite.org on logging
Event logging, etc...
Interpreting MSI log files, etc...
Credentials: Just adding that there are several built-in accounts you can use to run the service:
The difference between the 'Local System' account and the 'Network Service' account?
And there is the new concept of "managed service accounts". Step-by-Step.
And finally "group managed service account".

Why can't my WebAPI service call find part of the path to a folder via IIS?

I have .NET Core WebAPI 2.1 services. I created a structure in my deployment folder like below. (I'm using IIS.) I want to access MyVariables.json from all services. But, it gave this error:
Could not find a part of the path 'C:\wwwroot\MyProject\Shared\MyVariables.json'.
But that folder and path do exist. I'm trying by this code from my C# service.
using (StreamReader file =File.OpenText(#"../Shared/MyVariables.json"))
{
}
What can be the reason of this?
My publish folder design
-wwwroot
-MyProject
+Service1
+Service2
+Service3
+Service4
-Shared
MyVariables.json
IIS runs with an identity that is configured in the App pool.
Directory access rights must be given to the identity used to run the App pool.
Start inetmgr.exe to configure IIS,find App Pool identity and then configure access in file explorer.
Reference: https://learn.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities

Rotativa crashes on service fabric node in Azure

I have a project that needs to collect several data and put it in a single pdf, to do so I used Rotativa wrapper. It works fine on my local service fabric cluster and Im able to generate the pdf using ViewAsPdf and then getting the byte array to attach it in an email. The problem comes when I deployed the project in Azure (service fabric cluster) and Im getting this error:
at Rotativa.AspNetCore.WkhtmlDriver.Convert(String wkhtmlPath, String switches, String html, String wkhtmlExe)\r\n at Rotativa.AspNetCore.WkhtmltopdfDriver.ConvertHtml(String wkhtmltopdfPath, String switches, String html)\r\n at Rotativa.AspNetCore.ViewAsPdf.<CallTheDriver>d__19.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---
Things that Ive tried:
Installed Visual C++ Redistributable Packages for Visual Studio 2013 and 2015 in x64 and x86 versions in the service fabric node (where the project is being deployed)
Moved the Rotativa folder from wwwroot to the root path of the project and set the path in RotativaConfiguration.Setup() method
Copied msvcp120.dll and msvcr120.dll to the Rotativa folder
Installed wkhtmltopdf.exe in the service fabric node
I installed Rotativa.AspNetCore 1.0.6 nuget package
I use ViewAsPdf to render the razor view and I call BuildFile method to get the byte array
If I ran the wkhtmltopdf.exe in the command line in the service fabric node I get: "The application was unable to start correctly(0xc000007b) Click ok to close the application"
The service fabric node is a Windows machine. The project is built with aspnet core 2.0
Any help will be appreciated. Thanks!
I assume is a missing dependency issue.
Based on the scenario and message you provided The application was unable to start correctly(0xc000007b) there are other related problems:
The application was unable to start correctly (0xc000007b) and Error : the application was unable to start correctly
The other issue might be lack of permissions to run or access specific dependencies.
I would suggest you try:
running as administrator first via command line.
Check firewall or network\website permissions
Use dependency walker and check the dependencies it loads\fail and verify if they are installed on the server.
As a workaround you could create a container to run this. It would save you the trouble of going into the nodes to install stuff, which limits your scale out & fail-over options.
There are some people who got it working.

VSTS deploy: Unable to replace variables in appsettings.json

I have a fairly standard setup for an ASP.NET Core application - source control on GitHub with the live application hosted on Azure, with master branch changes triggering CI builds on VSTS.
The DB connection string is in appsettings.json, normally pointing at a localdb instance. For deploying to Azure, I want it to point to an Azure SQL DB (which I had neglected to set up during the first publish).
The app service itself was set up on Azure as an app service without a DB - in other words, I shouldn't have done this just after waking up. However, both the app service and DB are linked to the same resource group.
The problem I am facing is that whenever I trigger a CI build via VSTS, the build works but the release falls over at the point at which it tries to replace the connection string with the one defined in the CI build variables.
2017-07-09T08:47:02.7205410Z ##[section]Starting: Deploy Azure App Service
2017-07-09T08:47:02.7415413Z ==============================================================================
2017-07-09T08:47:02.7415413Z Task : Azure App Service Deploy
2017-07-09T08:47:02.7415413Z Description : Update Azure App Service using Web Deploy / Kudu REST APIs
2017-07-09T08:47:02.7415413Z Version : 3.3.9
2017-07-09T08:47:02.7415413Z Author : Microsoft Corporation
2017-07-09T08:47:02.7415413Z Help :
2017-07-09T08:47:02.7415413Z ==============================================================================
2017-07-09T08:47:06.5658468Z Got connection details for Azure App Service:'BlackscarsSheetsSwtor'
2017-07-09T08:47:07.6978989Z ##[error]Error: NO JSON file matched with specific pattern: appsettings,json.
I've tried the solution here, only to be met with exactly the same error, though with the log showing **/appsettings.json instead of appsettings.json.
Deploy definition
This is the appsettings.json file
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Telvee32.BlackscarsSheetsSwtor.Db;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
This is my first time working with build and release definitions so it's entirely possible I've fessed something up, but the actual definitions were created automatically by Azure and were working fine until I started doing this, which, given that the application is dependent upon a DB, definitely needs to be working properly.
My initial thoughts are that you've misspelt the JSON config name
Service:'BlackscarsSheetsSwtor' 2017-07-09T08:47:07.6978989Z ##[error]Error: NO JSON file matched with specific pattern: appsettings,json.
Here you have a comma , instead of a ..
Have you checked to see if that's the cause of the problem?

Deployment issue with Azure Emulator

I'm trying to run a local instance of an Azure Worker Role for debugging and testing, but for some reason, my Role will not deploy to the Emulator locally, it is saying, "The compute emulator had a error: Found too many .csdef files.."
The problem is, I checked the project directory, and there is only one csdef file...
I have already built the file as required with CSPack, and my CSRun command looks like this:
csrun.exe /run:C:\ODA
T\WorkerRole\WorkerRole;C:\ODAT\WorkerRole\WorkerRole\
ServiceDefinition.csdef
And this is being ran at the following director: C:\Program Files\Windows Azure Emulator\emulator
Any help is greatly appreciated!
Try using the cscfg file not the csdef and use the .csx directory for the emulator.
csrun C:\ODAT\WorkerRole\WorkerRole.csx C:\ODAT\WorkerRole\WorkerRole.cscfg
If you don't have a .cscfg file you can have cspack create it for you by specifying the /generateConfigurationFile:ServiceConfiguration.cscfg

Categories