How can I reuse the TraceWriter object available in an azure Function to log information in an externally referenced library? I tried passing in the object using the constructor and referencing the TraceWriter class (web.http.tracing). I didn't have luck as the classes seem different.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Short version Use the Microsoft.Azure.WebJobs.Host.TraceWriter available in this nuget package.
Alternatively, build your functions as a Web Project and you can debug locally. You can find a sample here.
Long Version
Your problem here is that you are using the wrong TraceWriter.
I used the Azure Function logger in an Azure function to output the type of the logger.
Which gave the following:
I too was expecting a Web/Http TraceWriter and was surprised that there is yet another implementation to deal with. Microsoft could really do with creating a standard approach, or at least giving us a nice clean interface for Error, Warning, Info, Verbose etc. Maybe something for .Net Standard... please.
I will create my own interface and wrap my app logger and the Azure one so that I can inject whichever I need without causing headaches further along in my code. This will also offer some protection from potential pain inflicted by future breaking changes.
Anyway, I digress, I then tracked
Microsoft.Azure.WebJobs.Script.InterceptingTraceWriter
down to the Azure Functions / Webjobs scripting GitHub repo and then on to the Nuget package. I have tested this and it works fine to pass the Azure Function logger in to your external assembly and continue to log to the Azure Function environment from there.Here is an example:
I love the potential of Azure functions, but it is still a little immature and overly complex.
I hope this helps.
Added a very simple single class logger to illustrate.
It writes to either the Azure Functions Logger or a standard Systems.Diagnostics.Trace. You need to paste this over the contents of Program.cs of a standard C# Console Application. You will also need to include the Nuget package Microsoft.Azure.WebJobs.
As an update, Azure Functions now support consuming an
ILogger
instead ofTraceWritter
so you could consume any logging framework that implementsILogger
.See the GitHub issue and the subsequent wiki documentation.
If I am correct the necessary versions for getting ILogger to work with Azure functions would be Microsoft.Azure.WebJobs 2.1.0-beta1. However, I can't seem to get an Azure function running using an ILogger instead of a TraceWriter.
Also very little information and documentation is available about developing Azure Functions using ILogger. Does anyone have more information or tips to get this working?
My C# code snippet:
I get the following error when debugging my Azure Function using the Azure Function Tools for VS2017: