2018-09-10 02:41:13 +02:00
|
|
|
// Copyright © 2017 The CefSharp Authors. All rights reserved.
|
2017-06-01 11:32:06 +10:00
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2021-01-09 14:17:50 +11:00
|
|
|
using System.Threading.Tasks;
|
2018-09-17 00:46:21 +02:00
|
|
|
using CefSharp.ModelBinding;
|
2017-06-01 11:32:06 +10:00
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
namespace CefSharp.Example.ModelBinding
|
2017-06-01 11:32:06 +10:00
|
|
|
{
|
2021-01-09 14:17:50 +11:00
|
|
|
public class MethodInterceptorLogger : IAsyncMethodInterceptor
|
2017-06-01 11:35:12 +10:00
|
|
|
{
|
2019-09-27 08:17:43 +10:00
|
|
|
object IMethodInterceptor.Intercept(Func<object[], object> method, object[] parameters, string methodName)
|
2017-06-01 11:35:12 +10:00
|
|
|
{
|
2021-01-09 14:17:50 +11:00
|
|
|
Debug.WriteLine("Before Method Call:" + methodName);
|
|
|
|
|
var result = method(parameters);
|
|
|
|
|
Debug.WriteLine("After Method Call:" + methodName);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Task<object> IAsyncMethodInterceptor.InterceptAsync(Func<object[], object> method, object[] parameters, string methodName)
|
|
|
|
|
{
|
|
|
|
|
var result = method(parameters);
|
|
|
|
|
|
|
|
|
|
if(result == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Let the CLR workout if it's a generic task or not.
|
|
|
|
|
return await InterceptAsync((dynamic)result, methodName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<object> InterceptAsync(Task task, string methodName)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Before Method Call:" + methodName);
|
|
|
|
|
|
|
|
|
|
await task.ConfigureAwait(false);
|
|
|
|
|
// do the logging here, as continuation work for Task...
|
|
|
|
|
Debug.WriteLine("After Method Call:" + methodName);
|
|
|
|
|
|
|
|
|
|
//As our task doesn't return a result we return null.
|
|
|
|
|
//We currently don't support an equivilent of undefined
|
|
|
|
|
//in javascript
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<T> InterceptAsync<T>(Task<T> task, string methodName)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Before Method Call:" + methodName);
|
|
|
|
|
T result = await task.ConfigureAwait(false);
|
|
|
|
|
Debug.WriteLine("After Method Call:" + methodName);
|
|
|
|
|
|
|
|
|
|
// do the logging here, as continuation work for Task<T>...
|
2017-06-01 11:35:12 +10:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-01 11:32:06 +10:00
|
|
|
}
|