我想知道你调用任何类或任何类方法的时间。有可能吗?
更新
例如
10-17 10:36:31.935 I/MonoDroid(31518): UNHANDLED EXCEPTION:
10-17 10:36:31.955 I/MonoDroid(31518): System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.PlatformNotSupportedException: The PCL build of Realm is being linked which probably means you need to use NuGet or otherwise link a platform-specific Realm.dll to your main application.
10-17 10:36:31.955 I/MonoDroid(31518): at Realms.RealmPCLHelpers.ThrowProxyShouldNeverBeUsed () [0x00000] in <filename unknown>:0
10-17 10:36:31.955 I/MonoDroid(31518): at Realms.Realm.GetInstance (Realms.RealmConfiguration config) [0x00000] in <filename unknown>:0
10-17 10:36:31.955 I/MonoDroid(31518): at MyPack.SidePage.MyPage..ctor () [0x00008] in C:\Users\User\documents\visual studio 2015\Projects\MyPack\MyPack\MyPack\SidePage\MyPage.xaml.cs:14
10-17 10:36:31.955 I/MonoDroid(31518): at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
10-17 10:36:31.955 I/MonoDroid(31518): at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:644
10-17 10:36:31.965 I/MonoDroid(31518): --- End of inner exception stack trace ---
10-17 10:36:31.965 I/MonoDroid(31518): at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00016] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:650
10-17 10:36:31.965 I/MonoDroid(31518): at System.RuntimeType.CreateInstanceMono (Boolean nonPublic) [0x000ca] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:115
10-17 10:36:31.965 I/MonoDroid(31518): at System.RuntimeType.CreateInstanceSlow (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x0001a] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:89
10-17 10:36:31.965 I/MonoDroid(31518): at System.RuntimeType.CreateInstanceDefaultCtor (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x0002a] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/rttype.cs:5599
10-17 10:36:31.965 I/MonoDroid(31518): at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00040] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/activator.cs:201
10-17 10:36:31.965 I/MonoDroid(31518): at System.Activator.CreateInstance (System.Type type) [0x00000] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/activator.cs:134
10-17 10:36:31.965 I/MonoDroid(31518): at MyPack.MainPage.OnItemSelected (System.Object sender, Xamarin.Forms.SelectedItemChangedEventArgs e) [0x00016] in C:\Users\User\documents\visual studio 2015\Projects\MyPack\MyPack\MyPack\MainPage.xaml.cs:24
10-17 10:36:31.965 I/MonoDroid(31518): at (wrapper delegate-invoke) System.EventHandler`1[Xamarin.Forms.SelectedItemChangedEventArgs]:invoke_void_object_TEventArgs (object,Xamarin.Forms.SelectedItemChangedEventArgs)
10-17 10:36:31.975 I/MonoDroid(31518): at Xamarin.Forms.ListView.OnSelectedItemChanged (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x0001c] in <filename unknown>:0
10-17 10:36:31.975 I/MonoDroid(31518): at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindablePropertyContext context, System.Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent) [0x0010e] in <filename unknown>:0
10-17 10:36:31.975 I/MonoDroid(31518): at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes) [0x0014b] in <filename unknown>:0
10-17 10:36:31.975 I/MonoDroid(31518): at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, SetValueFlags attributes) [0x00000] in <filename unknown>:0
10-17 10:36:31.975 I/MonoDroid(31518): at Xamarin.Forms.ListView.NotifyRowTapped (Int32 groupIndex, Int32 inGroupIndex, Xamarin.Forms.Cell cell) [0x0004c] in <filename unknown>:0
10-17 10:36:31.975 I/MonoDroid(31518): at Xamarin.Forms.ListView.NotifyRowTapped (Int32 index, Xamarin.Forms.Cell cell) [0x00023] in <filename unknown>:0
10-17 10:36:31.975 I/MonoDroid(31518): at Xamarin.Forms.Platform.Android.ListViewAdapter.HandleItemClick (Android.Widget.AdapterView parent, Android.Views.View view, Int32 position, Int64 id) [0x00056] in <filename unknown>:0
10-17 10:36:31.975 I/MonoDroid(31518): at Xamarin.Forms.Platform.Android.CellAdapter.OnItemClick (Android.Widget.AdapterView parent, Android.Views.View view, Int32 position, Int64 id) [0x0002e] in <filename unknown>:0
10-17 10:36:31.975 I/MonoDroid(31518): at Android.Widget.AdapterView+IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (IntPtr jnienv, IntPtr native__this, IntPtr native_parent, IntPtr native_view, Int32 position, Int64 id) [0x00019] in /Users/builder/data/lanes/3415/7db2aac3/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Widget.AdapterView.cs:215
10-17 10:36:31.975 I/MonoDroid(31518): at (wrapper dynamic-method) System.Object:585983a8-79f2-48a3-ac33-5b4c905219f1 (intptr,intptr,intptr,intptr,int,long)
10-17 10:36:31.995 D/Mono (31518): DllImport searching in: '__Internal' ('(null)').
10-17 10:36:31.995 D/Mono (31518): Searching for 'java_interop_jnienv_throw'.
10-17 10:36:31.995 D/Mono (31518): Probing 'java_interop_jnienv_throw'.
10-17 10:36:31.995 D/Mono (31518): Found as 'java_interop_jnienv_throw'.
An unhandled exception occured.
10-17 10:36:34.115 E/mono (31518):
10-17 10:36:34.115 E/mono (31518): Unhandled Exception:
10-17 10:36:34.115 E/mono (31518): System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.PlatformNotSupportedException: The PCL build of Realm is being linked which probably means you need to use NuGet or otherwise link a platform-specific Realm.dll to your main application.
10-17 10:36:34.115 E/mono (31518): at Realms.RealmPCLHelpers.ThrowProxyShouldNeverBeUsed () [0x00000] in <filename unknown>:0
10-17 10:36:34.115 E/mono (31518): at Realms.Realm.GetInstance (Realms.RealmConfiguration config) [0x00000] in <filename unknown>:0
10-17 10:36:34.115 E/mono (31518): at MyPack.SidePage.MyPage..ctor () [0x00008] in C:\Users\User\documents\visual studio 2015\Projects\MyPack\MyPack\MyPack\SidePage\MyPage.xaml.cs:14
10-17 10:36:34.115 E/mono (31518): at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
10-17 10:36:34.115 E/mono (31518): at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:644
10-17 10:36:34.115 E/mono (31518): --- End of inner exception stack trace ---
10-17 10:36:34.115 E/mono (31518): at (wrapper dynamic-method) System.Object:585983a8-79f2-48a3-ac33-5b4c905219f1 (intptr,intptr,intptr,intptr,int,long)
10-17 10:36:34.115 E/mono (31518): at (wrapper native-to-managed) System.Object:585983a8-79f2-48a3-ac33-5b4c905219f1 (intptr,intptr,intptr,intptr,int,long)
10-17 10:36:34.115 E/mono-rt (31518): [ERROR] FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.PlatformNotSupportedException: The PCL build of Realm is being linked which probably means you need to use NuGet or otherwise link a platform-specific Realm.dll to your main application.
10-17 10:36:34.115 E/mono-rt (31518): at Realms.RealmPCLHelpers.ThrowProxyShouldNeverBeUsed () [0x00000] in <filename unknown>:0
10-17 10:36:34.115 E/mono-rt (31518): at Realms.Realm.GetInstance (Realms.RealmConfiguration config) [0x00000] in <filename unknown>:0
10-17 10:36:34.115 E/mono-rt (31518): at MyPack.SidePage.MyPage..ctor () [0x00008] in C:\Users\User\documents\visual studio 2015\Projects\MyPack\MyPack\MyPack\SidePage\MyPage.xaml.cs:14
10-17 10:36:34.115 E/mono-rt (31518): at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
10-17 10:36:34.115 E/mono-rt (31518): at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:644
10-17 10:36:34.115 E/mono-rt (31518): --- End of inner exception stack trace ---
10-17 10:36:34.115 E/mono-rt (31518): at (wrapper dynamic-method) System.Object:585983a8-79f2-48a3-ac33-5b4c905219f1 (intptr,intptr,intptr,intptr,int,long)
10-17 10:36:34.115 E/mono-rt (31518): at (wrapper native-to-managed) System.Object:585983a8-79f2-48a3-ac33-5b4c905219f1 (intptr,intptr,intptr,intptr,int,long)
In mgmain JNI_OnLoad
更新
我尝试实现一个基于Ruby的Observe Pattern的观察者,但是返回错误......
class Example
def obeserver_method
p "this class has been called."
end
end
这是错误:require "observer"
class AAnyClass
extend Observable
changed
end
module AnObserver
extend self
def call constant
p "Constant #{constant} has been called."
end
def observe constant
constant.add_observer(self, call(constant))
end
end
#=> returns "Constant AAnyClass has been called."
AnObserver.observe AAnyClass
# must return "Constant AAnyClass has been called."
AAnyClass
# must return "Constant AAnyClass has been called."
AAnyClass
更新
新实施
`add_observer': observer does not respond to `Constant AAnyClass has been called.' (NoMethodError)
不会返回错误,但看不到任何内容。
答案 0 :(得分:1)
您可以使用logger
:
class Example
def obeserver_method
# some logic
Rails.logger.info "this class has been called."
end
end
然后,在您的日志文件中,您将看到有关方法调用的信息。
来自评论:
抱歉,假设在终端上运行rails c,并假设我的 项目的模型
Example
包含属性id
,attr1
,attr2
..., 当我输入Example
并按Enter键时,我希望观察者运行 例程,例如,连接到特定的数据库。
即使我没有清楚地看到如何在控制台中键入Example
是某些操作的良好触发器,但无论如何,您基本上在您的问题中命名了该工具:观察者。
查看Observable
class,轻松实现 Observer模式。
答案 1 :(得分:0)
如果您正在寻找一种方法来跟踪整个应用程序中的所有方法调用,而无需解析日志文件或添加记录器调用,则可以安装Coverband。 Coverband会对您在rails应用程序中调用的所有方法进行非常详细的跟踪。您可以按源文件查看统计数据并向下钻取。
答案 2 :(得分:0)
定义一个类方法,然后调用它......
class Example
def self.instantiate
puts "Hello World!"
end
instantiate
end
在控制台中测试...
$ rails c
Loading development environment (Rails 3.2.22.4)
2.2.5 :001 > Example
Hello World!
=> Example