首页 \ 问答 \ 在ILoggerFactory中使用Application Insights(Using Application Insights with ILoggerFactory)

在ILoggerFactory中使用Application Insights(Using Application Insights with ILoggerFactory)

我正在尝试将例外记录到Application Insights。 我通过直接调用TelemetryClient.TrackException成功完成此操作。 但是,我想在我的代码中抽象出这种情况,以防将来要登录到其他平台,所以我只想遵守ILogger接口。

我发现你可以使用ILoggerFactory.AddApplicationInsights (在这里实现),但不管我做了什么,我都没有看到ApplicationInsights中显示的日志。

以下是我的代码:

Startup.cs

    IConfigurationRoot Configuration { get; set; }
    ILoggerFactory LoggerFactory { get; set; }
    IServiceProvider ServiceProvider { get; set; }

    public Startup( IHostingEnvironment hostingEnvironment, ILoggerFactory loggerFactory )
    {
        this.LoggerFactory = loggerFactory;
        string configurationFilePath = "abc.json";

        this.Configuration = new ConfigurationBuilder()
            .SetBasePath( hostingEnvironment.ContentRootPath )
            .AddJsonFile( configurationFilePath, optional: true, reloadOnChange: true )
            .AddEnvironmentVariables()
            .Build();
    }

    public void Configure(
        IApplicationBuilder applicationBuilder,
        IHostingEnvironment hostingEnvironment,
        ILoggerFactory loggerFactory,
        IServiceProvider serviceProvider )
    {
        this.ServiceProvider = serviceProvider;
        loggerFactory.AddApplicationInsights( serviceProvider );
        applicationBuilder.UseMvc();
    }

    public void ConfigureServices( IServiceCollection services )
    {
        services.AddApplicationInsightsTelemetry( this.Configuration );
        services.AddMvc( .... // A bunch of options here ... )
    }

然后,我尝试在我的控制器中使用这个:

    ILogger<XController> Logger { get; set; }

    public XController( ILogger<XController> logger )
    {
        this.Logger = logger;
    }

    [HttpPost]
    [Route( "v3.0/abcd" )]
    public async Task PostEvent( [FromBody] XEvent xEvent )
    {
        this.Logger.LogError( 0, new Exception( "1234" ), "1234" );
    }

但是,我根本没有看到与请求相关的任何异常。 如果我用TelemetryClient.TrackException替换Logger.LogError行(并首先创建TelemetryClient ),那么我可以看到没有任何问题的异常。

我不知道我做错了什么。 谁能帮忙?


I'm trying to log exceptions to Application Insights. I succeeded in doing this by calling TelemetryClient.TrackException directly. However, I would like to abstract away from this in my code in case I'd want to log to other platforms in the future, so I would like to stick to only the ILogger interface.

I found that you can use ILoggerFactory.AddApplicationInsights (as implemented here) but no matter what I did, I don't see the logs showing up in ApplicationInsights with this.

Below is my code:

Startup.cs

    IConfigurationRoot Configuration { get; set; }
    ILoggerFactory LoggerFactory { get; set; }
    IServiceProvider ServiceProvider { get; set; }

    public Startup( IHostingEnvironment hostingEnvironment, ILoggerFactory loggerFactory )
    {
        this.LoggerFactory = loggerFactory;
        string configurationFilePath = "abc.json";

        this.Configuration = new ConfigurationBuilder()
            .SetBasePath( hostingEnvironment.ContentRootPath )
            .AddJsonFile( configurationFilePath, optional: true, reloadOnChange: true )
            .AddEnvironmentVariables()
            .Build();
    }

    public void Configure(
        IApplicationBuilder applicationBuilder,
        IHostingEnvironment hostingEnvironment,
        ILoggerFactory loggerFactory,
        IServiceProvider serviceProvider )
    {
        this.ServiceProvider = serviceProvider;
        loggerFactory.AddApplicationInsights( serviceProvider );
        applicationBuilder.UseMvc();
    }

    public void ConfigureServices( IServiceCollection services )
    {
        services.AddApplicationInsightsTelemetry( this.Configuration );
        services.AddMvc( .... // A bunch of options here ... )
    }

Then, I try to use this in my controller like this:

    ILogger<XController> Logger { get; set; }

    public XController( ILogger<XController> logger )
    {
        this.Logger = logger;
    }

    [HttpPost]
    [Route( "v3.0/abcd" )]
    public async Task PostEvent( [FromBody] XEvent xEvent )
    {
        this.Logger.LogError( 0, new Exception( "1234" ), "1234" );
    }

However, I don't see any exceptions associated with the request at all. If I replace the Logger.LogError line with TelemetryClient.TrackException (and create the TelemetryClient first), then I can see the exception without any issues.

I don't know what I'm doing wrong. Could anyone help?


原文:https://stackoverflow.com/questions/45022693
更新时间:2023-05-31 17:05

最满意答案

更新3

CELLSJAVA-42597现在被标记为已解决(无法修复)。 请参阅Aspose.Cells论坛中的此主题以获取更多信息。


更新2

我们执行你的代码并得到以下输出。 我们已将此问题记录在我们的数据库中以供进一步调查和分析。 有一次,我们会为你提供一些消息,我们会尽快更新你的消息。

此问题已记录为

  • CELLSJAVA-42597 - Aspose.Cells在处理特定文件后不释放内存

控制台输出:

Total= 491 MB, free= 490 MB
Total= 491 MB, free= 490 MB
-----------------------------
Entering consumeMemory 
-----------------------------
Total= 491 MB, free= 471 MB
Total= 491 MB, free= 240 MB
Total= 491 MB, free= 240 MB
Total= 491 MB, free= 240 MB
Total= 491 MB, free= 240 MB
Total= 491 MB, free= 240 MB
Total= 491 MB, free= 240 MB
-----------------------------
Exiting consumeMemory 
-----------------------------
Total= 491 MB, free= 490 MB
Total= 491 MB, free= 490 MB
-----------------------------
Processing : Bad.xlsx
-----------------------------
Total= 491 MB, free= 475 MB
Total= 491 MB, free= 450 MB
Total= 444 MB, free= 361 MB
Total= 463 MB, free= 311 MB
Total= 475 MB, free= 321 MB
Total= 488 MB, free= 270 MB
Total= 492 MB, free= 340 MB
-----------------------------
Done. Output : Bad.xlsx.pdf
-----------------------------
Total= 494 MB, free= 348 MB
Total= 498 MB, free= 352 MB
Total= 497 MB, free= 351 MB
Total= 499 MB, free= 353 MB
Total= 499 MB, free= 353 MB
Total= 501 MB, free= 355 MB
Total= 500 MB, free= 354 MB
Total= 503 MB, free= 357 MB
Total= 502 MB, free= 356 MB
Total= 504 MB, free= 358 MB
Total= 503 MB, free= 357 MB
Total= 505 MB, free= 359 MB

更新1

我们建议您阅读以下可帮助您解决内存问题的文章。

此外,你必须使用我们最新的版本,因为我们也做了关于内存消耗的增强。 最近的版本是18.4,即将在几天内发布。 但是你也可以在18.3上试试它。


它也可能是Java堆的问题,而不是Aspose的问题。

你能否在没有Aspose的情况下复制这个问题? 例如,在您的线程中,您可以创建500 MB的字节数组,并让内存堆增长,直到显示为您的输出为止,然后通过将空值分配给字节数组释放500 MB内存,并且您的输出应该反映堆内存已经降低或释放了哪些(用你的话来说)Aspose没有发生。


注意:我在Aspose担任开发者推广人员


Update-3

CELLSJAVA-42597 is marked as resolved (won't fix) now. Please refer to this topic in Aspose.Cells forum for more information.


Update-2

We executed your code and got the following output. We have logged this issue in our database for further investigation and analysis. Once, we will have some news for you, we will update you asap.

This issue has been logged as

  • CELLSJAVA-42597 - Aspose.Cells not releasing memory after processing a particular file

Console Output:

Total= 491 MB, free= 490 MB
Total= 491 MB, free= 490 MB
-----------------------------
Entering consumeMemory 
-----------------------------
Total= 491 MB, free= 471 MB
Total= 491 MB, free= 240 MB
Total= 491 MB, free= 240 MB
Total= 491 MB, free= 240 MB
Total= 491 MB, free= 240 MB
Total= 491 MB, free= 240 MB
Total= 491 MB, free= 240 MB
-----------------------------
Exiting consumeMemory 
-----------------------------
Total= 491 MB, free= 490 MB
Total= 491 MB, free= 490 MB
-----------------------------
Processing : Bad.xlsx
-----------------------------
Total= 491 MB, free= 475 MB
Total= 491 MB, free= 450 MB
Total= 444 MB, free= 361 MB
Total= 463 MB, free= 311 MB
Total= 475 MB, free= 321 MB
Total= 488 MB, free= 270 MB
Total= 492 MB, free= 340 MB
-----------------------------
Done. Output : Bad.xlsx.pdf
-----------------------------
Total= 494 MB, free= 348 MB
Total= 498 MB, free= 352 MB
Total= 497 MB, free= 351 MB
Total= 499 MB, free= 353 MB
Total= 499 MB, free= 353 MB
Total= 501 MB, free= 355 MB
Total= 500 MB, free= 354 MB
Total= 503 MB, free= 357 MB
Total= 502 MB, free= 356 MB
Total= 504 MB, free= 358 MB
Total= 503 MB, free= 357 MB
Total= 505 MB, free= 359 MB

Update-1

We recommend you to read the following article that could help you resolve memory issue.

Besides, you must use our most recent version because we have also done enhancements regarding memory consumption. The most recent version is 18.4 which is about to be released in couple of days. But you can try it on 18.3 as well.


It could also be the issue of Java heap probably and not the issue of Aspose.

Can you replicate the issue without Aspose? For example, in your thread, you can create 500 MB array of bytes and let the memory heap grow until it shows up as in your output and then free the 500 MB memory by assigning null to byte array and your output should reflect that heap memory has lowered or released which (in your words) not happening with Aspose.


Note: I am working as Developer Evangelist at Aspose

相关问答

更多
  • 您的问题似乎是ImageLoader包含一个缓存并正在向其添加所有图像。 如果您不需要该功能,并且它看起来不像您那样,那么您应该删除它并简化ImageLoader以便它只下载并返回图像。 Your problem appears to be that ImageLoader contains a cache and is adding all of the images to it. If you don't need that functionality, and it doesn't really lo ...
  • 您必须配置Instruments以保留免费事件。 首先,确保未选中“释放存储空闲事件”复选框。 其次,将分配寿命设置为Created和Destroyed。 要查看Instruments中的免费事件,您必须深入查看特定的内存地址。 乐器不提供免费活动列表。 在分配摘要中,将鼠标光标移到类别上。 将出现带箭头的按钮。 单击该按钮可查看该类别的内存分配列表。 将鼠标光标移动到内存地址上以显示带箭头的另一个地址。 单击按钮以查看该地址的所有内存事件。 在这里您可以找到免费活动。 You have to config ...
  • TL; DR解决方案:确保使用return返回每个函数以确保所有局部变量都从ram中销毁** Per Pavel的建议,我使用了内存跟踪器(不幸的是,建议的mem跟踪器对我没用 ,所以我使用了Pympler 。) 实施相当简单: from pympler.tracker import SummaryTracker tracker = SummaryTracker() ~~~~~~~~~YOUR CODE tracker.print_diff() 跟踪器提供了一个很好的输出,这很明显,函数生成的局部变量 ...
  • 许多答案都说你必须打电话给Dispose。 虽然这些答案意味着很好,但它们实际上并没有帮助你。 您正在调用Close,而Close和Dispose执行相同的操作。 使用“使用”块是一种更好的做法,这样你就可以自动为你调用Dispose,但是你的代码很好。 你问题的真正答案是“不要再担心了”。 你在错误的层面上考虑这个问题。 我假设您正在查看任务管理器中的“工作集”或“私有字节”,但您可能无法理解这些实际意味着什么。 大多数人没有。 这个答案给出了一个很好的总结 什么是专用字节,虚拟字节,工作集? 好的,既然 ...
  • 更新3 CELLSJAVA-42597现在被标记为已解决(无法修复)。 请参阅Aspose.Cells论坛中的此主题以获取更多信息。 Aspose单元在处理特定文件后不释放内存 更新2 我们执行你的代码并得到以下输出。 我们已将此问题记录在我们的数据库中以供进一步调查和分析。 有一次,我们会为你提供一些消息,我们会尽快更新你的消息。 此问题已记录为 CELLSJAVA-42597 - Aspose.Cells在处理特定文件后不释放内存 控制台输出: Total= 491 MB, free= 490 MB T ...
  • 由imageNamed:加载的UIImage imageNamed:不会自动释放(它们被缓存,具有相同名称的图像共享相同的数据)。 使用imageWithContentsOfFile:或imageWithData:scale:如果您希望自动释放图像。 请注意,您可以使用NSBundle方法获取图像路径,尽管它会更复杂一些。 您必须自己实施以下部分 如果屏幕的比例为2.0,则此方法首先搜索具有相同文件名的图像文件,并附加@ 2x后缀。 例如,如果文件名是按钮,则首先搜索按钮@ 2x。 如果找到2x,则加载该图 ...
  • 有几件事在这里玩。 虚拟内存 首先,您需要明白,仅仅因为您的程序“使用”5 GB的内存并不意味着其他程序只剩下3 GB的内存。 虚拟内存意味着这5 GB可能只有1 GB的实际“常驻”数据,其他4 GB实际上可能在磁盘上,而不是在RAM中。 因此,当您查看您的程序时,请务必查看“常驻套餐尺寸”而不是“虚拟尺寸”。 请注意,如果您的系统实际上RAM不足,操作系统可能会通过“分页”部分内存来缩小某些程序的RSS。 因此,不要过多担心系统监视器中出现“5 GB” - 担心是否存在真正的具体性能问题。 堆分配 第二个 ...
  • 您正在谈论垃圾收集 ,这是从内存中删除不再需要的对象的过程。 你可以在这里很好地了解Java的工作原理。 在Android(以及一般的Java)中,您可以使用System.gc()调用建议虚拟机执行此操作。 但请注意,它: 向VM表明运行垃圾收集器是一个好时机。 请注意,这只是一个提示。 无法保证垃圾收集器实际上会运行。 You are talking about Garbage Collection, which is the process of removing from the memory the ...
  • 当没有对该对象的更多引用时,将释放Python对象。 将obj重新绑定为None会减少对象的引用计数,因此del obj也是如此。 在这两种情况下 ,如果obj是对它的最后一个引用,则将清除该实例。 不同之处在于你以后如何使用obj 。 重新绑定保留变量(它现在绑定到None ), del完全删除它,你会得到一个NameError 。 你选择的是你的选择,它不会影响实例从内存中清除的方式。 Python objects are released when there are no more referenc ...
  • 虽然我不知道你的MainWindow.Open/CloseWindow()方法是什么样的,但我想我有一个这个问题的复制品。 使用线程肯定是问题的一部分,有一个非直观的事情你必须做,以防止泄漏具有线程亲和力的内部WPF管道对象。 必须关闭该线程的调度程序。 这通常只是在UI线程终止时在WPF应用程序中发生一次。 同样非常重要的是调度员进行调度,WPF在很大程度上依赖于它以确保“弱事件”实际上很弱。 MainWindow的一个示例实现,它不会泄漏: public class MainWindow : Windo ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。