Serilog configuration via appsettings.json for Application Insights - configuration

I am trying to use https://github.com/serilog/serilog-settings-configuration to read app settings and setup serilog for app insights: https://github.com/serilog/serilog-sinks-applicationinsights. The issue I am having is that I cannot set the last parameters for ApplicationInsightsEvents call, which is a function that takes LogEvent and returns ITelemetry. How can this be set via appsettings.json?
Basically, I want to replace the followoing line:
log.WriteTo.ApplicationInsightsEvents(instrumentationKey, level, CultureInfo.CurrentCulture, TelemetryConverter.ConvertLogEventsToEnerGovTelemetry);
with a line inside appsettings.json
Thanks.

Add a sink configuration to appsettings.json
{
"Name": "ApplicationInsights",
"Args": {
"instrumentationKey": "<instrumentationKey>",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights",
"outputTemplate": "[{Component}|{MachineName}|{ThreadId}] {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] <{SourceContext}> {Message:lj}{NewLine}{Exception}"
}
}
And
"Serilog.Sinks.ApplicationInsights"
to "Serilog:Using" array

Answered on GitHub: https://github.com/serilog/serilog-settings-configuration/issues/165. Just need to write an assembly and embed the final code there.

Related

How to pass a list as an environment variable to an AWS Lambda function from a JSON config file?

I have a JSON file that is going to contain a number of different lists per client that I am deploying for. These lists are going to serve as container overrides for an ECS task that my Lambda function will be invoking. The JSON config file would look something like this:
{
"clientName": {
"environment": [
{
"name": "name1",
"value": "value1"
}
]
}
}
And my serverless.yml would look something like this:
environment:
CONTAINER_ENVIRONMENT: ${file(serverlessConfig.json):${env:CLIENT_NAME}.environment}
Which results in the following error:
Could not resolve "CONTAINER_ENVIRONMENT" environment variable: Unsupported environment variable format:
[
{
"name": "name1",
"value": "value1"
}
]
I've tried using CloudFormation intrinsic functions such as Fn::Join and Fn::ToJsonString. These both threw an error when trying to run locally using sls invoke local (the errors were the same as the above). After some digging it seems that these functions aren't compatible with the serverless environment property.
The only thing that has worked so far is storing the list as a string in a .env file, but that's not really ideal since these configs could take a number of different environment objects.
Is there any way to get this to work with the setup that I have going?

Can I create a config file for Parse Server?

I'm quite new to Parse Server, I wonder if I could create a config file for the Parse Server like the way I did in the Parse Dashboard. With the Parse Dashboard, I create a file named parse-server-config.json with this format:
{
"apps": [
{
"serverURL": "http://10.30.176.147:1337/parse",
"appId": "myFirstApp",
"masterKey": "myMasterKey",
"databaseURI": "mongodb://mongo/team"
}
]
}
And start the dashboard using parse-server --config parse-server-config.json, but I couldn;t do that with the Parse Server, I have to use this command parse-server --appId myFirstApp --masterKey myMasterKey --databaseURI mongodb://localhost:27017/team?readPreference=primary&appname=MongoDB%20Compass&ssl=false --mountGraphQL --mountPlayground to start the Parse server.
Yes. You need to use the following command:
parse-server path/to/config.json
Your config.json should look like this:
{
"appId": "APPLICATION_ID",
"masterKey": "MASTER_KEY",
"databaseURI": "mongodb://localhost/test"
}
You can see all configuration options in the following link:
http://parseplatform.org/parse-server/api/master/ParseServerOptions.html

Read values from local.settings.json in VS 2017 Azure Function development

I am writing an Azure function in VS 2017. I need to set up a few custom configuration parameters. I added them in local.settings.json under Values.
{
"IsEncrypted":false,
"Values" : {
"CustomUrl" : "www.google.com",
"Keys": {
"Value1":"1",
"Value2" :"2"
}
}
}
Now, ConfigurationManager.AppSettings["CustomUrl"] returns null.
I'm using:
.NET Framework 4.7
Microsoft.NET.Sdk.Functions 1.0.5
System.Configuration.ConfigurationManager 4.4.0
Azure.Functions.Cli 1.0.4
Am I missing something?
Environment.GetEnvironmentVariable("key")
I was able to read values from local.settings.json using the above line of code.
Firstly, I create a sample and do a test with your local.settings.json data, as Mikhail and ahmelsayed said, it works fine.
Besides, as far as I know, Values collection is expected to be a Dictionary, if it contains any non-string values, it can cause Azure function can not read values from local.settings.json.
My Test:
ConfigurationManager.AppSettings["CustomUrl"] returns null with the following local.settings.json.
{
"IsEncrypted": false,
"Values": {
"CustomUrl": "www.google.com",
"testkey": {
"name": "kname1",
"value": "kval1"
}
}
}
If you are using TimeTrigger based Azure function than you can access your key (created in local.settings.json) from Azure Function as below.
[FunctionName("BackupTableStorageFunction")]
public static void Run([TimerTrigger("%BackUpTableStorageTriggerTime%")]TimerInfo myTimer, TraceWriter log, CancellationToken cancellationToken)
Using .Net 6 (and probably some earlier versions) it is possible to inject IConfiguration into the constructor of the function.
public Function1(IConfiguration configuration)
{
string setting = _configuration.GetValue<string>("MySetting");
}
MySetting must be in the Values section of local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"MySetting": "value"
}
}
It works with Application settings in Azure Function App as well.
Azure function copies the binaries to the bin folder and runs using the azure function cli, so it searches for the local.settings.json, so make sure you have set the "Copy to Output Directory" to "Copy Always"
Hey you mmight be able to read the properties while debugging, but once you go and try to deploy that in azure, those properties are not going to work anymore. Azure functions does not allow nested properties, you must use all of them inline in the "Values" option or in "ConnectionStrings".
Look at this documentation as reference:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings
var value = Environment.GetEnvironmentVariable("key", EnvironmentVariableTarget.Process); should be the more appropriate answer, though EnvironmentVariableTarget.Process is the default value but it's more meaningful here.
Look at its EnvironmentVariableTarget declaration.
//
// Summary:
// Specifies the location where an environment variable is stored or retrieved in
// a set or get operation.
public enum EnvironmentVariableTarget
{
//
// Summary:
// The environment variable is stored or retrieved from the environment block associated
// with the current process.
Process = 0,
//
// Summary:
// The environment variable is stored or retrieved from the HKEY_CURRENT_USER\Environment
// key in the Windows operating system registry. This value should be used on .NET
// implementations running on Windows systems only.
User = 1,
//
// Summary:
// The environment variable is stored or retrieved from the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session
// Manager\Environment key in the Windows operating system registry. This value
// should be used on .NET implementations running on Windows systems only.
Machine = 2
}

Octopus Deploy JSON Configuration Variable Feature not working

I must be missing something simple with this JSON Configuration Variable feature. I have the following JSON:
{
"EventProcessorFactory": "Factory"
"HubConfiguration": [
{
"ArchiveStorageConnectionString": "Connection string"
"ArchiveStorageContainerName": "containerName"
"EventHubGroupName": "ehs-peds-dev"
}
],
"HeartBeatConfiguration": {
"EventHubGroupName": "GroupName",
"EventHubName": "HubName"
"SharedAccessKey": "AccessKey"
"SharedAccessKeyName": "EventPublishers",
"EndPointURL": "URL"
}
}
I want to replace the ArchiveStorageConnectionString (and other properties of HubConfiguration) but I can't figure out what the correct syntax is. HubConfiguration is an array of HubConfigurations (although in this case I only have 1).
I've tried
HubConfiguration:1:ArchiveStorageConnectionString
But that doesn't work. I've also tried
HubConfiguration:0:ArchiveStorageConnectionString
0:HubConfiguration:0:ArchiveStorageConnectionString
1:HubConfiguration:1:ArchiveStorageConnectionString
but none of this seem to work. Any pointers would be greatly appreciated.
The Octopus code that matches and replaces the variables is here:
https://github.com/OctopusDeploy/Calamari/blob/949008eaaafb8865305c7760e8f8448a5eaabb1a/source/Calamari/Integration/JsonVariables/JsonConfigurationVariableReplacer.cs
Some unit tests are here:
https://github.com/OctopusDeploy/Calamari/blob/949008eaaafb8865305c7760e8f8448a5eaabb1a/source/Calamari.Tests/Fixtures/JsonVariables/JsonConfigurationVariableReplacerFixture.cs
I would have expected this to work:
HubConfiguration:0:ArchiveStorageConnectionString
There does seem to be a problem with your JSON - this line should end with a ,:
"EventProcessorFactory": "Factory"
Do you get any output in your deployment when the convention runs?

Adding a namespace to Laravel 4

I'm having a problem using a namespace in Laravel4.
We have built an API using Laravel3, in which we created an entire namespaced directory called Components which the RESTful Laravel controllers accessed to perform the logic on each request. The Components namespace was created in this manner so as to allow us to re-use the logic across several applications to keep things DRY.
In Laravel3, in the application/start.php file it was a simple matter of adding:
Autoloader::namespaces(array(
'Components' => 'path\to\Components',
));
This allowed us to simply reference a static method then in any of our RESTful controllers simply by
$result = Components\Services\Common::method();
In Laravel4, it is obviously a different approach. I have added the following to the composer.json file
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Components": "path/to/API/Components"
}
},
and ran the composer dump-autoload command to add the namespace to the autoload_namespaces.php file.
However, I cannot reference the namespace in any of my new controllers in Laravel4. I just get a "Class 'Components\Services\Common' not found" in HomeController.php.
I have checked in the autoload_real.php file and output the loader variable, where my new namespace is listed under the 'C' element of the array. But no joy in using it.
I know the namespace works as it is in constant use with our Laravel3 applciation. I would rather not replicate the directory into our new Laravel4 application, otherwise the reason we designed things this way will be negated and we'll end up maintaining two codebases. The namespace directory exists within our web root directory but outside of both our Laravel3 and Laravel4 applications.
Thanks for the help guys
If your namespace is
Components
And your application is in
/var/www/application
And your namespaced classes are inside the subfolder
app/API
And this is an example of class file name:
/var/www/application/app/API/Components/Services/Common.php
Then you have to add to your composer json:
"autoload": {
"psr-0": {
"Components": "app/API"
}
},
If you are loading from another base path and your namespaced classes are in /var/www/Components, you can:
"autoload": {
"psr-0": {
"Components": "/var/www"
}
},
But if they are in /var/www/components/Components, then you have to
"autoload": {
"psr-0": {
"Components": "/var/www/components"
}
},
Because "Components" is the base of your namespaces and will always be added to the path before Composer search files to autoload.