Serilog接收器未从Lambda记录

时间:2020-07-02 17:53:36

标签: c# logging .net-core aws-lambda serilog

对于我的WebAPI,.UseSerilog()似乎可以在本地框中运行的.Net Core 3.1 api的LocalEntryPoint上按预期工作。但是,当我使用LambdaEntryPoint的IWebHostBuilder部署到AWS Lambda时,似乎没有记录任何内容。我的LambdaEntryPoint的IWebhostBuilder是:

################### MIS_MATCH() ###################
def find_mismatch(each_word,item):
    if len(each_word) != len(item):
              return 2
    each_word=each_word.lower()
    item=item.lower()
    number_of_mismatches=0
    for index in range(len(each_word)):
                if each_word[index] != item[index]:
                    number_of_mismatches=number_of_mismatches+1
                    if number_of_mismatches>1:
                        return 2
    return number_of_mismatches
find_mismatch(each_word,item)           
################### SINGLE_INSERT_OR_DELETE()########


def single_insert_or_delete(s1,s2):
    s1=s1.lower()
    s2=s2.lower()
    if s1==s2:
        return 0
    if abs(len(s1)-len(s2))!=1:
        return 2
    if len(s1)>len(s2):
       # only deletion is possible
        for k in range(len(s2)):
            if s1[k]!=s2[k]:
                if s1[k+1:]==s2[k:]:
                    return 1
                else:
                    return 2
        return 1
    else:    # s1 is shorter Only insertion is possible
        for k in range(len(s1)):
            if s1[k]!=s2[k]:
                if s1[k:]==s2[k+1:]:
                    return 1
                else:
                    return 2
        return 1
single_insert_or_delete(s1,s2)      
    # main calculation 

for each_word in words:
    # if word is a 1 or 2 digit so just copy it to output and go to next word
    if len(each_word)==1 or len(each_word)==2 :
        output_list.append(each_word)
        continue
    #if a word is exactly the same as the list so again copy paste it to ...
    if each_word in ref:
        output_list.append(each_word)
        continue
    # to call inner function of single _insert in a loop
    for item in ref:
        if single_insert_or_delete(each_word,item)==1 or find_mismatch(each_word,item)==1:
            output_list.append(item)
            break
        else:
            output_list.append(each_word)
            break       
print (ref)
print (words)
print(output_list)

是否有人在AWS Lambda部署的应用程序中找到使用serilog进行记录的方法?

1 个答案:

答案 0 :(得分:0)

您将要像这样直接使用IWebHostBuilder的.UseSerilog()扩展名方法:

protected override void Init(IWebHostBuilder builder)
{
     builder
         .UseSerilog()
         .UseStartup<Startup>();
}

我的理解是,这将覆盖Microsoft.Extensions.Logging命名空间的默认ILogger实现。

然后在Startup类的ConfigureServices(IServiceCollection services)方法中,将实现Serilog.Core.Logger的单例,如下所示:

var loggerConfig = new LoggerConfiguration()
                .ReadFrom.Configuration(Configuration)
                .Enrich.WithThreadId()
                .Enrich.FromLogContext()
                .Enrich.WithProperty("Application", "MyApplicationName")
                .Enrich.WithProperty("Machine", Environment.MachineName)
                .WriteTo.Console(new RenderedCompactJsonFormatter());

var logger = loggerConfig.CreateLogger();
            services.AddSingleton<Logger>(logger);

这时,您已经将记录器依赖性注入到aspnetcore应用程序中,并可以在像这样的控制器中使用它:

private readonly Logger _log;

public DefaultController(Logger logger)
{
     _log = logger;
}

[HttpGet]
public IActionResult Get()
{
     _log.Information("This is an example of an information log");
     return Ok(new string[] { "value1", "value2" });
}