Export (0) Print
Expand All

EventLog.LogNameFromSourceName Method

Gets the name of the log to which the specified source is registered.

[Visual Basic]
Public Shared Function LogNameFromSourceName( _
   ByVal source As String, _
   ByVal machineName As String _
) As String
[C#]
public static string LogNameFromSourceName(
 string source,
 string machineName
);
[C++]
public: static String* LogNameFromSourceName(
 String* source,
 String* machineName
);
[JScript]
public static function LogNameFromSourceName(
   source : String,
 machineName : String
) : String;

Parameters

source
The name of the event source.
machineName
The name the computer on which to look, or an empty string ("") for the local computer.

Return Value

The name of the log associated with the specified source in the registry.

Remarks

The event source indicates what logs the event. It is often the name of the application, or the name of a subcomponent of the application, if the application is large. Applications and services should write to the Application log or a custom log. Device drivers should write to the System log.

When you create a new source, which can only write to one log at a time, the system registers your application with the event log as a valid source of entries. The Source property can be any string, but the name cannot be used by other sources on the computer. An attempt to create a duplicated Source value throws an exception. However, a single event log can have many different sources writing to it.

Example

[Visual Basic, C#, C++] The following example deletes a source from the local computer. The example determines the log from its source, and then deletes the log.

[Visual Basic, C#, C++] Note   More than one source might write to an event log. Before deleting a custom log, make sure there are no other sources writing to that log.
[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        Dim logName As String
        
        If EventLog.SourceExists("MyOldSource") Then
            ' Find the log associated with this source.    
            logName = EventLog.LogNameFromSourceName("MyOldSource", ".")
            
            ' Delete the source and the log.
            EventLog.DeleteEventSource("MyOldSource")
            EventLog.Delete(logName)
            
            Console.WriteLine((logName & " deleted."))
        End If
    End Sub 'Main
End Class 'MySample

[C#] 
using System;
using System.Diagnostics;
using System.Threading;
              
class MySample{

    public static void Main(){

         string logName;
       
        if(EventLog.SourceExists("MyOldSource")){
        // Find the log associated with this source.    
            logName = EventLog.LogNameFromSourceName("MyOldSource", ".");

        // Delete the source and the log.
            EventLog.DeleteEventSource("MyOldSource");
            EventLog.Delete(logName);
            
            Console.WriteLine(logName + " deleted.");    
        }                                                                        
    } 
}
   

[C++] 
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;

int main(){

    String* logName;

    if(EventLog::SourceExists(S"MyOldSource")){
        // Find the log associated with this source.    
        logName = EventLog::LogNameFromSourceName(S"MyOldSource", S".");

        // Delete the source and the log.
        EventLog::DeleteEventSource(S"MyOldSource");
        EventLog::Delete(logName);

        Console::WriteLine(S"{0} deleted.", logName);    
    }                                                                        
} 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

EventLog Class | EventLog Members | System.Diagnostics Namespace | Source | MachineName | Log | SourceExists

Show:
© 2015 Microsoft