Partilhar via


How to: Create Log Files 

You can create log files with diagnostic information about interoperability, loading the program, and networking. You can enable logging by setting the registry keys. First, set a registry key to enable general logging, and then set the registry keys for the desired logging component and options. The .NET Compact Framework version 2.0 provides support of using the Registry and RegistryKey classes.

The following table summarizes the log files:

Logging component Log file contents

Interop

Logs COM Interop calls. Provides information about platform invoke calls and marshaling.

Error

Logs all unhandled and native exceptions. Errors are logged to a log file and to OutputDebugString. The log file is created for each assembly at the current path and applies to the current session. After the first occurrence of an unhandled or native exception, the log file will be overwritten.

Loader

The file header contains the following:

  • Application name.

  • Process ID (as provided by Windows CE).

  • Local date and time the log file created. Format is not global or cultural specific.

  • .NET Compact Framework version, such as 2.0.5021.00.

  • Platform related information, such as Windows CE v5.0.1400 (CEPC) WinCE5x86 debug Dev i386 IJITv2.

The file provides the following information:

  • Coercion state (compatibility mode).

  • Trust level assigned to modules as they are loaded.

  • Failure to resolve method.

  • Failure to resolve type.

  • Failure to find/load assembly or module.

  • Success in assembly loading.

  • Invalid metadata version.

  • Failure to find PInvoke DLL.

  • Failure to find function within PInvoke DLL.

  • Name of the policy file or the fact of its absence.

  • Major errors during policy file processing.

  • Policy-based redirection of the managed assemblies.

You can also include GAC related logging.

Networking

  • Logs network traffic.

  • Potential information disclosure, preceded by a confirmation prompt.

  • The networking log file is binary and cannot be accessed. Information about viewing this file will be made available in the future.

  • Because networking logging occurs at the Winsock layer, the information only contains network packet information so no sensitive user data could be obtained.

By default, log files are written to the same directory containing the application being diagnosed. However, you can you can specify a path and other options with registry keys as follows:

  • Use an alternate path to write the log files. This requires privileged access to the secure registry.

  • Include the application name in the log file name.

  • Include the process ID in the log file name.

A log file has the following parts, where component is either Interop, Loader, or Network. The application name and process ID are optional and are specified with registry settings.

"netcf_" + (Application name) + (component) + (Process ID) + ".log". For example, a Loader log file for "MyApp.exe" with the application name and process ID would be:

netcf_MyApp_Loader_2066923010.log

For information about examining the log file, see Log File Information.

To enable logging

  • Set the Enabled key value 1:

HKLM\Software\Microsoft\.NETCompactFramework\Diagnostics\Logging\Enabled

This key value must be set to enable the three types of logging: Interop, Loader, and Networking. Note that the sub keys under Logging do not exist by default.

You can turn off all logging by setting this value to zero.

To specify a path for the log file (optional)

  • Set the Path key value to a string:

HKLM\Security\.NETCompactFramework\Diagnostics\Logging\Path

This key is only accessible by applications that can write to the secure registry. If a path is not specified, the log file is written to the same directory containing the application.

To include the application in the name (optional)

  • Set the UseApp key value to 1:

HKLM\Software\Microsoft\.NETCompactFramework\Diagnostics\Logging\UseApp

This key is useful if you want to run multiple applications and get separate log files. If there are two applications writing log files to the same directory, the older log file will always get overwritten with the newer log file when the second application is run. This key can be used as a differentiator for the log file.

To include the process ID in the name (optional)

  • Set the UsePid key value to 1:

HKLM\Software\Microsoft\.NETCompactFramework\Diagnostics\Logging\UsePid

This key is useful if you want to run the same application but have separate logs. This adds the process ID to the log file name, so that each run of the same application creates a new log file with a different name.

To log events as they occur (optional)

  • Set the Flush key value to 1:

HKLM\Software\Microsoft\.NETCompactFramework\Diagnostics\Logging\Flush

This value causes the common language runtime to write log events to the log file as they occur instead of keeping them in the buffer and writing then when the buffer is full. Flushing negatively affects performance of the application and might modify timing of the application slightly since every piece of logging information is written out immediately, but it can be useful to diagnose problems related to application crashes or other errors where you might want to get the last few logs that resulted in the crash. If this key is not present or not set, then the default operation is to not flush.

Interop Logging

To enable Interop logging

  • Set the Enabled key value to 1:

HKLM\Software\Microsoft\.NETCompactFramework\Diagnostics\Logging\Interop\Enabled

Error Logging

To enable Error logging

  • Set the Enabled value to 1:

HKLM\Software\Microsoft\.NETCompactFramework\Diagnostics\Logging\Error\Enabled

Loader Logging

To enable Loader logging

  • Set Enabled value to 1 to enable Loader logging, or set to 2 to enable Loader and GAC related logging.

HKLM\Software\Microsoft\.NETCompactFramework\Diagnostics\Logging\Loader\Enabled

Networking Logging

To enable Networking logging

  • Set the Enabled value to 1:

HKLM\Software\Microsoft\.NETCompactFramework\Diagnostics\Logging\Networking\Enabled

The networking log file is binary and cannot be read without the parser that translates from the binary format to human readable format. Information about viewing this file will be available on the .NET Compact Framework site on MSDN.

Example

You can set values using a registry editor or write an application. The following code example contains methods that do the needed registry tasks.

  • The EnableLogging method enables general logging and contains parameters to specify a path, and boolean values where true includes the application name, process ID, and events to the log.

  • The SetInteropLogging, SetLoaderLogging, and SetNetworkingLogging methods set their Enabled key value to 1, so that logging for the component is enabled.

  • The DisableLogging method disables all logging.

  • The WriteLogSettings method recursively examines the keys under the Logging sub key and writes their key names and values to a log file. It saves to "logsettings.txt" in the directory containing this example application.

' This method enables general logging and contains parameters
' to specify a path, and boolean values where true includes
' the application name, process id, and events to the log.
Private Sub EnableLogging(ByVal useApp As Boolean, ByVal usePid As Boolean, ByVal useFlush As Boolean) 
    ' Specify values for setting the registry.
    Dim userRoot As String = "HKEY_LOCAL_MACHINE"
    Dim subkey As String = "SOFTWARE\Microsoft\.NETCompactFramework\Diagnostics\Logging"
    Dim keyName As String = userRoot + "\" + subkey
    
    ' Set the the Enabled registry value.
    Registry.SetValue(keyName, "Enabled", 1)
    
    If useApp = True Then
        Registry.SetValue(keyName, "UseApp", 1)
    Else
        Registry.SetValue(keyName, "UseApp", 0)
    End If 
    If usePid = True Then
        Registry.SetValue(keyName, "UsePid", 1)
    Else
        Registry.SetValue(keyName, "UsePid", 0)
    End If 
    If useFlush = True Then
        Registry.SetValue(keyName, "UseFlush", 1)
    Else
        Registry.SetValue(keyName, "UseFlush", 0)
    End If

End Sub
 
' This method set the Enabled key value to 1,
' so that logging for Interoperability is enabled.
Private Sub SetInteropLogging(ByVal logOn As Boolean) 
    ' Specify values for setting the registry.
    Dim userRoot As String = "HKEY_LOCAL_MACHINE"
    Dim subkey As String = "Software\Microsoft\.NETCompactFramework\Diagnostics\Logging\Interop"
    Dim keyName As String = userRoot + "\" + subkey
    
    Dim logSet As Integer
    If logOn = True Then
        logSet = 1
    Else
        logSet = 0
    End If 
    ' Set the the registry value.
    Try
        Registry.SetValue(keyName, "Enabled", logSet)
        If logOn = True Then
            MsgBox("Interop Logging On")
        Else
            MsgBox("Interop Loggin Off")
        End If
    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try

End Sub


' This set the Enabled key value to 1,
' so that logging for class loading is enabled.
Private Sub SetLoaderLogging(ByVal logOn As Boolean) 
    ' Specify values for setting the registry.
    Dim userRoot As String = "HKEY_LOCAL_MACHINE"
    Dim subkey As String = "Software\Microsoft\.NETCompactFramework\Diagnostics\Logging\Loader"
    Dim keyName As String = userRoot + "\" + subkey
    
    Dim logSet As Integer
    If logOn = True Then
        logSet = 1
    Else
        logSet = 0
    End If 
    ' Set the the registry value.
    Try
        Registry.SetValue(keyName, "Enabled", logSet)
        If logOn = True Then
            MsgBox("Loader Logging On")
        Else
            MsgBox("Loader Loggin Off")
        End If
    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try

End Sub


' This method set the Enabled key value to 1,
' so that logging for networking is enabled.
Private Sub SetNetworkLogging(ByVal logOn As Boolean) 
    ' Specify values for setting the registry.
    Dim userRoot As String = "HKEY_LOCAL_MACHINE"
    Dim subkey As String = "Software\Microsoft\.NETCompactFramework\Diagnostics\Logging\Networking"
    Dim keyName As String = userRoot + "\" + subkey
    
    Dim logSet As Integer
    If logOn = True Then
        logSet = 1
    Else
        logSet = 0
    End If 
    ' Set the the registry value.
    Try
        Registry.SetValue(keyName, "Enabled", logSet)
        If logOn = True Then
            MsgBox("Networking Logging On")
        Else
            MsgBox("Networking Loggin Off")
        End If
    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try

End Sub


' This method disables all logging.
Private Sub DisableLogging() 
    ' Specify values for setting the registry.
    Dim userRoot As String = "HKEY_LOCAL_MACHINE"
    Dim subkey As String = "SOFTWARE\Microsoft\.NETCompactFramework\Diagnostics\Logging"
    Dim keyName As String = userRoot + "\" + subkey
    
    ' Set the the Enabled registry value.
    Registry.SetValue(keyName, "Enabled", 0)
    MsgBox("Logging Disabled")

End Sub


' This method recursively examines the keys
' under the Logging sub key and writes their
' key names and values to a log file. It saves
' to "logsettings.txt" in the directory
' containing this example application.
Private Sub WriteLoggingSettings() 
    Dim sw As New StreamWriter("logsettings.txt", False)
    sw.WriteLine("General Logging Settings:")
    Dim rkLogging As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\.NETCompactFramework\Diagnostics\Logging")
    Dim valNames As String() = rkLogging.GetValueNames()
    Dim x As Integer
    For x = 0 To valNames.Length
        sw.WriteLine(valNames(x).ToString() + ": " + rkLogging.GetValue(valNames(x)).ToString())
    Next x

    sw.WriteLine()
    sw.WriteLine("Interop Logging:")
    Dim rkInterop As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\.NETCompactFramework\Diagnostics\Logging\Interop")
    Dim interopNames As String() = rkInterop.GetValueNames()

    For x = 0 To interopNames.Length
        sw.WriteLine(interopNames(x).ToString() + ": " + rkInterop.GetValue(interopNames(x)).ToString())
    Next x

    sw.WriteLine()
    sw.WriteLine("Loader Logging:")
    Dim rkLoader As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\.NETCompactFramework\Diagnostics\Logging\Loader")
    Dim loaderNames As String() = rkLoader.GetValueNames()
    For x = 0 To loaderNames.Length
        sw.WriteLine(loaderNames(x).ToString() + ": " + rkLoader.GetValue(loaderNames(x)).ToString())
    Next x

    sw.WriteLine()
    sw.WriteLine("Networking Logging:")
    Dim rkNetworking As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\.NETCompactFramework\Diagnostics\Logging\Networking")
    Dim netNames As String() = rkNetworking.GetValueNames()
    For x = 0 To netNames.Length
        sw.WriteLine(netNames(x).ToString() + ": " + rkNetworking.GetValue(netNames(x)).ToString())
    Next x
    sw.Close()
End Sub
// This method enables general logging and contains parameters
// to specify a path, and boolean values where true includes
// the application name, process id, and events to the log.
private void EnableLogging(bool useApp, bool usePid, bool useFlush)
{
    // Specify values for setting the registry.
    string userRoot = "HKEY_LOCAL_MACHINE";
    string subkey = "SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging";
    string keyName = userRoot + "\\" + subkey;

    // Set the the Enabled registry value.
    Registry.SetValue(keyName, "Enabled", 1);

    if (useApp == true)
        Registry.SetValue(keyName, "UseApp", 1);
    else
        Registry.SetValue(keyName, "UseApp", 0);

    if (usePid == true)
        Registry.SetValue(keyName, "UsePid", 1);
    else
        Registry.SetValue(keyName, "UsePid", 0);

    if (useFlush == true)
        Registry.SetValue(keyName, "UseFlush", 1);
    else
        Registry.SetValue(keyName, "UseFlush", 0);
}

// This method set the Enabled key value to 1,
// so that logging for Interoperability is enabled.
private void SetInteropLogging(bool logOn)
{
    // Specify values for setting the registry.
    string userRoot = "HKEY_LOCAL_MACHINE";
    string subkey = "Software\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Interop";
    string keyName = userRoot + "\\" + subkey;

    int logSet;
    if(logOn == true)
      logSet = 1;
    else
      logSet = 0;

    // Set the the registry value.
    try
    {
     Registry.SetValue(keyName, "Enabled", logSet);
     if(logOn == true)
        MessageBox.Show("Interop Logging On");
     else
        MessageBox.Show("Interop Loggin Off");
     }
     catch(System.Exception ex)
     {
        MessageBox.Show(ex.Message);
     }
}

// This set the Enabled key value to 1,
// so that logging for class loading is enabled.
private void SetLoaderLogging(bool logOn)
{
    // Specify values for setting the registry.
    string userRoot = "HKEY_LOCAL_MACHINE";
    string subkey = "Software\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Loader";
    string keyName = userRoot + "\\" + subkey;

    int logSet;
    if(logOn == true)
    logSet = 1;
    else
    logSet = 0;

    // Set the the registry value.
    try
    {
        Registry.SetValue(keyName, "Enabled", logSet);
        if(logOn == true)
        MessageBox.Show("Loader Logging On");
        else
        MessageBox.Show("Loader Loggin Off");
    }
    catch(System.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

// This method set the Enabled key value to 1,
// so that logging for networking is enabled.
  private void SetNetworkLogging(bool logOn)
  {
    // Specify values for setting the registry.
    string userRoot = "HKEY_LOCAL_MACHINE";
    string subkey = "Software\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Networking";
    string keyName = userRoot + "\\" + subkey;

    int logSet;
    if(logOn == true)
      logSet = 1;
    else
      logSet = 0;

    // Set the the registry value.
    try
    {
         Registry.SetValue(keyName, "Enabled", logSet);
         if(logOn == true)
            MessageBox.Show("Networking Logging On");
         else
            MessageBox.Show("Networking Loggin Off");
     }
     catch(System.Exception ex)
     {
        MessageBox.Show(ex.Message);
     }
  }

// This method disables all logging.
private void DisableLogging()
{
    // Specify values for setting the registry.
    string userRoot = "HKEY_LOCAL_MACHINE";
    string subkey = "SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging";
    string keyName = userRoot + "\\" + subkey;

    // Set the the Enabled registry value.
    Registry.SetValue(keyName, "Enabled", 0);
    MessageBox.Show("Logging Disabled");
}

// This method recursively examines the keys
// under the Logging sub key and writes their
// key names and values to a log file. It saves
// to "logsettings.txt" in the directory
// containing this example application.
private void WriteLoggingSettings()
{
    StreamWriter sw = new StreamWriter("logsettings.txt",false);
    sw.WriteLine("General Logging Settings:");
    RegistryKey rkLogging = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging");
    string[] valNames = rkLogging.GetValueNames();
    for (int x = 0; x < valNames.Length; x++)
    {
        sw.WriteLine(valNames[x].ToString() + ": " + rkLogging.GetValue(valNames[x]).ToString());
    }

    sw.WriteLine();
    sw.WriteLine("Interop Logging:");
    RegistryKey rkInterop = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Interop");
    string[] interopNames = rkInterop.GetValueNames();
    for (int x = 0; x < interopNames.Length; x++)
    {
        sw.WriteLine(interopNames[x].ToString() + ": " + rkInterop.GetValue(interopNames[x]).ToString());
    }

    sw.WriteLine();
    sw.WriteLine("Loader Logging:");
    RegistryKey rkLoader = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Loader");
    string[] loaderNames = rkLoader.GetValueNames();
    for (int x = 0; x < loaderNames.Length; x++)
    {
        sw.WriteLine(loaderNames[x].ToString() + ": " + rkLoader.GetValue(loaderNames[x]).ToString());
    }

    sw.WriteLine();
    sw.WriteLine("Networking Logging:");
    RegistryKey rkNetworking = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Networking");
    string[] netNames = rkNetworking.GetValueNames();
    for (int x = 0; x < netNames.Length; x++)
    {
        sw.WriteLine(netNames[x].ToString() + ": " + rkNetworking.GetValue(netNames[x]).ToString());
    }
   sw.Close();
}

Compiling the Code

This example requires references to the following namespaces:

See Also

Concepts

Log File Information

Other Resources

Performance and Diagnostics