将日志从log4net记录到Azure函数中的Azure表存储中 [英] log exceptions from log4net to azure table storage in azure function

查看:321
本文介绍了将日志从log4net记录到Azure函数中的Azure表存储中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是这个链接将异常从log4net记录到Azure表存储中.

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

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

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.

推荐答案

现在,我需要在.net core中的Azure函数中执行相同的操作,但是由于Azure函数中没有配置文件,因此无法使用那里一样吗?

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

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"

  }
}

local.settings.json读取您在Azure函数上的属性:

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

调试和测试:

请参见下面的屏幕截图:

See the screen shot below:

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

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!

这篇关于将日志从log4net记录到Azure函数中的Azure表存储中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆