示例#1
0
        public static void EvtSetChannelConfigProperty(EventLogHandle handle, Microsoft.Win32.UnsafeNativeMethods.EvtChannelConfigPropertyId enumType, object val)
        {
            EventLogPermissionHolder.GetEventLogPermission().Demand();
            Microsoft.Win32.UnsafeNativeMethods.EvtVariant propertyValue = new Microsoft.Win32.UnsafeNativeMethods.EvtVariant();
            CoTaskMemSafeHandle handle2 = new CoTaskMemSafeHandle();
            using (handle2)
            {
                bool flag;
                if (val == null)
                {
                    goto Label_017B;
                }
                switch (enumType)
                {
                    case Microsoft.Win32.UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigEnabled:
                        propertyValue.Type = 13;
                        if (!((bool) val))
                        {
                            break;
                        }
                        propertyValue.Bool = 1;
                        goto Label_0183;

                    case Microsoft.Win32.UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigAccess:
                        propertyValue.Type = 1;
                        handle2.SetMemory(Marshal.StringToCoTaskMemAuto((string) val));
                        propertyValue.StringVal = handle2.GetMemory();
                        goto Label_0183;

                    case Microsoft.Win32.UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigRetention:
                        propertyValue.Type = 13;
                        if (!((bool) val))
                        {
                            goto Label_0146;
                        }
                        propertyValue.Bool = 1;
                        goto Label_0183;

                    case Microsoft.Win32.UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigAutoBackup:
                        propertyValue.Type = 13;
                        if (!((bool) val))
                        {
                            goto Label_016B;
                        }
                        propertyValue.Bool = 1;
                        goto Label_0183;

                    case Microsoft.Win32.UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigMaxSize:
                        propertyValue.Type = 10;
                        propertyValue.ULong = (ulong) ((long) val);
                        goto Label_0183;

                    case Microsoft.Win32.UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigLogFilePath:
                        propertyValue.Type = 1;
                        handle2.SetMemory(Marshal.StringToCoTaskMemAuto((string) val));
                        propertyValue.StringVal = handle2.GetMemory();
                        goto Label_0183;

                    case Microsoft.Win32.UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigLevel:
                        propertyValue.Type = 8;
                        propertyValue.UInteger = (uint) ((int) val);
                        goto Label_0183;

                    case Microsoft.Win32.UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigKeywords:
                        propertyValue.Type = 10;
                        propertyValue.ULong = (ulong) ((long) val);
                        goto Label_0183;

                    default:
                        throw new InvalidOperationException();
                }
                propertyValue.Bool = 0;
                goto Label_0183;
            Label_0146:
                propertyValue.Bool = 0;
                goto Label_0183;
            Label_016B:
                propertyValue.Bool = 0;
                goto Label_0183;
            Label_017B:
                propertyValue.Type = 0;
            Label_0183:
                flag = Microsoft.Win32.UnsafeNativeMethods.EvtSetChannelConfigProperty(handle, enumType, 0, ref propertyValue);
                int errorCode = Marshal.GetLastWin32Error();
                if (!flag)
                {
                    EventLogException.Throw(errorCode);
                }
            }
        }
        public static void EvtSetChannelConfigProperty(EventLogHandle handle, UnsafeNativeMethods.EvtChannelConfigPropertyId enumType, object val) {

            EventLogPermissionHolder.GetEventLogPermission().Demand();

            UnsafeNativeMethods.EvtVariant varVal = new UnsafeNativeMethods.EvtVariant();

            CoTaskMemSafeHandle taskMem = new CoTaskMemSafeHandle();

            using (taskMem) {

                if (val != null) {

                    switch (enumType) {
                        case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigEnabled: {
                                varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeBoolean;
                                if ((bool)val == true) varVal.Bool = 1;
                                else varVal.Bool = 0;
                            }
                            break;
                        case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigAccess: {
                                varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeString;
                                taskMem.SetMemory(Marshal.StringToCoTaskMemAuto((string)val));
                                varVal.StringVal = taskMem.GetMemory();
                            }
                            break;
                        case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigLogFilePath: {
                                varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeString;
                                taskMem.SetMemory(Marshal.StringToCoTaskMemAuto((string)val));
                                varVal.StringVal = taskMem.GetMemory();
                            }
                            break;
                        case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigMaxSize: {
                                varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt64;
                                varVal.ULong = (ulong)((long)val);
                            }
                            break;
                        case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigLevel: {
                                varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt32;
                                varVal.UInteger = (uint)((int)val);
                            }
                            break;
                        case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigKeywords: {
                                varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt64;
                                varVal.ULong = (ulong)((long)val);
                            }
                            break;
                        case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigRetention: {
                                varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeBoolean;
                                if ((bool)val == true) varVal.Bool = 1;
                                else varVal.Bool = 0;
                            }
                            break;
                        case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigAutoBackup: {
                                varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeBoolean;
                                if ((bool)val == true) varVal.Bool = 1;
                                else varVal.Bool = 0;
                            }
                            break;
                        default:
                            throw new InvalidOperationException();
                    }
                }
                else {
                    varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeNull;
                }
                bool status = UnsafeNativeMethods.EvtSetChannelConfigProperty(handle, enumType, 0, ref varVal);
                int win32Error = Marshal.GetLastWin32Error();
                if (!status)
                    EventLogException.Throw(win32Error);
            }
        }