Differences

This shows you the differences between the selected revisions of the page.

2019-11-19 2019-11-19
error handling and logging (martin) version 2.0 (martin)
Line 5: Line 5:
The only problem you will face is, that for some reason, when publishing the project to an Azure storage, ''winscp.exe'' dependency is stored to the root folder of the function, instead of the ''bin'' subfolder along with other binaries (particularly the ''WinSCPnet.dll''). The only problem you will face is, that for some reason, when publishing the project to an Azure storage, ''winscp.exe'' dependency is stored to the root folder of the function, instead of the ''bin'' subfolder along with other binaries (particularly the ''WinSCPnet.dll'').
-To overcome that, you need to use [[library_session#executablepath|''Session.ExecutablePath'']] to point to the actual location of ''winscp.exe''. You can use ''ExecutionContext.FunctionAppDirectory'' to refer to the root folder of the function. To get an instance of the [[https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#executioncontext|''ExecutionContext'']], add new parameter to the signature of [[https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#methods-recognized-as-functions|your entry method]] (usually ''Run''):+To overcome that, you need to use [[library_session#executablepath|''Session.ExecutablePath'']] to point to the actual location of ''winscp.exe''. You can use ''ExecutionContext.FunctionAppDirectory'' to refer to the root folder of the function. To get an instance of the [[https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#executioncontext|''ExecutionContext'']], add new parameter to the signature of [[https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#methods-recognized-as-functions|your entry method]] (usually ''Run'')
 + 
 +The following example is for version 2.x of Azure Functions:
<code csharp> <code csharp>
[FunctionName("Function1")] [FunctionName("Function1")]
public static void Run( public static void Run(
-    [TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log, ExecutionContext context)+    [TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log, ExecutionContext context)
{ {
    try     try
    {     {
-        log.Info("Starting");+        log.LogInformation("Starting"); 
        SessionOptions sessionOptions = new SessionOptions();         SessionOptions sessionOptions = new SessionOptions();
        // setup sessionOptions         // setup sessionOptions
Line 23: Line 26:
            session.ExecutablePath = Path.Combine(context.FunctionAppDirectory, "winscp.exe");             session.ExecutablePath = Path.Combine(context.FunctionAppDirectory, "winscp.exe");
-            log.Info("Connecting");+            log.LogInformation("Connecting");
            session.Open(sessionOptions);             session.Open(sessionOptions);
-············ + 
-            // ...··+            // ...
        }         }
-        log.Info("Done");+        log.LogInformation("Done");
    }     }
    catch (Exception e)     catch (Exception e)
    {     {
-        log.Error("Error", e);+        log.LogError(e, "Error");
    }     }
} }
</code> </code>
 +For version 1.x of Azure functions, the code is basically the same. Just for logging, you need to use ''TraceWriter'' instead of ''ILogger''. Read about  [[https://docs.microsoft.com/en-us/azure/azure-functions/functions-versions#migrating-a-locally-developed-application|Migrating from 1.x to later versions]].

Last modified: by martin