将日志从log4net记录到Azure函数中的Azure表存储中

时间:2019-06-11 09:12:00

标签: asp.net-core azure-functions log4net azure-table-storage

我指的是这个link来记录从log4net到Azure表存储的异常。

现在,我需要在.net核心中的azure函数中执行相同的操作,但是由于azure函数中没有配置文件,因此无法在其中使用相同的功能。

感谢任何帮助以获取任何参考。

1 个答案:

答案 0 :(得分:0)

现在我需要在.net核心中的azure函数中执行相同的操作,但是由于azure函数中没有配置文件,因此无法在其中使用相同的文件?

似乎您正在尝试读取通常从配置文件中读取的某些属性。是的,您也可以在Azure Function中进行操作。文件名为local.settings.json,您可以从此处读取所需的属性。请参见下面的示例:

local.settings.json:

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

  }
}

local.settings.json读取Azure功能上的属性:

  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;
    }
}

调试和测试:

请参见以下屏幕截图:

enter image description here

  

注意:您甚至可以将local.settings.json文件中的值之外的属性设置为单独的属性

如果您仍有任何问题,请随时分享。谢谢,祝您编程愉快!