log exceptions from log4net to azure table storage

2019-07-09 04:36发布

I am referring this link to log exceptions from log4net to azure table storage.

Now i need to do the same in azure functions in .net core, but since there is no config files in azure functions am not able to use the same there.

Any help appreciated to get any reference on this.

1条回答
爷的心禁止访问
2楼-- · 2019-07-09 05:14

Now i need to do the same in azure functions in .net core, but since there is no config files in azure functions am not able to use the same there?

Seems you are trying to read some property what we usually read from config file. Yes you can do it in Azure Function also. There is file name local.settings.json you can read your required property from here. See the example below:

local.settings.json:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "TableName": "YourTableName",
    "AccountName": "YourAccountName",
    "AccountKey": "YourAccountKey"

  }
}

Read Your Property On Azure Function From local.settings.json:

  public static class AzureFunctionApp2Arunraj414CaseForGetConfigProperty
{
    [FunctionName("AzureFunctionApp2Arunraj414CaseForGetConfigProperty")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        //Read Request Body
        var content = await new StreamReader(req.Body).ReadToEndAsync();

        //Extract Request Body and Parse To Class
        Users objUsers = JsonConvert.DeserializeObject<Users>(content);

        //You Can Read Your Desired Value from local.settings.json file Like Below
        var yourTableNameFromLocalSettingsJson = Environment.GetEnvironmentVariable("TableName");
        var yourAccountNameFromLocalSettingsJson = Environment.GetEnvironmentVariable("AccountName");
        var yourAccountKeyFromLocalSettingsJson = Environment.GetEnvironmentVariable("AccountKey");


        //I am returning all the property I got from local.settings.json
        var result = new OkObjectResult(yourTableNameFromLocalSettingsJson +" & "+ yourAccountNameFromLocalSettingsJson + " & " + yourAccountKeyFromLocalSettingsJson+ " From local.settings.json");
        return result;
    }
}

Debug & Testing:

See the screen shot below:

enter image description here

Note: You even can set your property outside of values on local.settings.json file as separate property

If you still have any problem feel free to share. Thanks and happy coding!

查看更多
登录 后发表回答